Search code examples
jqueryajaxjsonstruts2struts2-json-plugin

Ajax GET returned JSON object shows as [object, Object]


I am using Struts2 and ajax. Ajax get function returns a json. But, if I print the returned json using "alert" or console, it shows [object, Object]. I have used dataType:"json" in the ajax call too. Can someone please point out what could be missing?

$.ajax({
        type:"GET",
        data: "searchClientPhone=" + searchClientPhone,
        url: "searchClientCellPhoneNo",
        dataType: "json",
        headers : {
            Accept : "application/json; charset=utf-8", 
            "Content-Type" : "application/json; charset=utf-8"
        },
        success: function(result){

        alert("result: " + result);
        /* var response = $.parseJSON(result);
        alert("response is  : " + response);
        console.log("Response : " + response); */
        console.log("Result " + result);

        $("#selectedClientName").html(result.selectedClientName);
    $("#selectedClientRewardPoints").html(result.selectedClientRewardPoints);
            $("#progressbar").hide();
            $("#example td").each( function() {
                 var thisCell = $(this);
                 var cellValue = parseInt(thisCell.text());

                 if (!isNaN(cellValue) && (cellValue >= document.getElementById("selectedClientRewardPoints").value)) {
                     thisCell.css("background-color","#FF0000");
                  }
              }
             );

            $("#selectedClientName").show();        
            $("#selectedClientRewardPoints").show();

        }
    }); 

I have even tried using parseJSON but it gives me an error "unexpected token o" which on searching seems to be error if the returned result is already parsed.

Thanks in advance!


Solution

  • You need to stringify your JSON to view it in an alert.

    alert("response is  : " + JSON.stringify(response));