Angelo
Angelo

Reputation: 427

json parse not working

I am getting this response from my server with ajax call

var data = 
[{
     "Response": {
        "ResponseStatus": {
            "Code": "1",
            "Description": "Success"
        },
        "TransactionReference": {}
    }
}, {
    "Response": {
        "ResponseStatus": {
            "Code": "1",
            "Description": "Success"
        },
        "TransactionReference": {}
    }
}];

Ajax call:

$.ajax({
              "type":"POST",
              "url":"'.CHtml::normalizeUrl(array("packaging/calltag")).'",
              "data":$("#returnrequestcreationform").serialize(),
              "success":function(data){
                  $.each(data, function (key, val) {
                      console.log(key + val);
                  });
              },
 });

But in the log, it gives me this

0[
1{
2"
3R
4e
5s
6p
7o
8n

I have tried JSON.parse() and jQuery.parseJSON() which give me "unexpected end of input" error when trying to read this object. It looks like a proper json return to me? Any idea why it is looping through it as if it is a string?

Upvotes: 0

Views: 394

Answers (2)

Funk Doc
Funk Doc

Reputation: 1643

You need to tell the function what the data type is

$.ajax({
          "type":"POST",
          "url":"'.CHtml::normalizeUrl(array("packaging/calltag")).'",
          "data":$("#returnrequestcreationform").serialize(),
           "dataType": "json",
          "success":function(data){
              $.each(data, function (key, val) {
                  console.log(key + val);
              });
          },
 });

Upvotes: 1

Scott Saunders
Scott Saunders

Reputation: 30394

Use the dataType option for your query ajax call to tell it you expect a json response.

http://api.jquery.com/jquery.ajax/

Upvotes: 0

Related Questions