Convert AJAX data to JSON

I am trying to get the data in a JSON object (which I checked is correctly formatted) and output the data to the firebug console. I checked JSON with JSONLint (http://jsonlint.com/) and I know that the data is not returned in the JSON object, because when I register it, it is registered as text, not an object. When I look at the ajax post, there is a JSON tab and it shows an object, I just can't get it for some reason.

My ajax call

    $.ajax({
        url:'/coords/base',
        data: { type: obj.type, id: obj.id },
        dataType:'text',
        type:'get',
        async:false,
        success: function(data) {
            console.log(data);
        }
    });

My return data looks like this:

    {   
        "1": {"name":"TEXT","coords":[        
            { "entry":3,"x":15,"y":15 }     
        ]}}

AJAX .responseText; AJAX. , $.serialize() $.parseJSON(), "uncaught exception: , ".

responseText JSON . , .

.

+3
2

jQuery :

$.ajax({
  // ...
  dataType: "text",
  // ...
});

JSON. , JS , : jQuery . dataType "json" jQuery , data, success:, JSON.

+3

$. getJson()

 $.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

:

$.getJSON('file.json', function(data) {
$.each(data, function(i) {
       console.log(data[i]);
     });
    });

, .

β„– 2 , : $getJSON, ?

:

$.ajax({
    type: 'GET',
    url: 'whatever',
    dataType: 'json',
    success: function(data) { console.log(data);},
    data: {},
    async: false
});

, , , , , .

+1

All Articles