0

I have an ajax query written in jQuery that is returning valid JSON in this format

$.ajax({
   type     : 'POST',
   url      : 'ajax/job/getActiveJobs.php',
   success  : function(data){
       if(data[''] === true){
           alert('json decoded');
       }

       $('#waiting').hide(500);
       $('#tableData').html(data['content']);
       $('#message').removeClass().addClass((data.error === true)
       ?'error':'success').text(data.msg);
       if(data.error === true)
           $('#message')

   },
   error    : function(XMLHttpRequest, textStatus, errorThrown){
       $('#waiting').hide(500);
       $('#message').removeClass().addClass('error').html(data.msg);
   } })

I take it this is not correct as it is not displaying the data, if I use

$('#mydiv').html(data);

I get all of the data back and displayed.

any help is really appreciated

Deviland
  • 3,324
  • 7
  • 32
  • 53
  • 1
    It would help a lot if you'd post the complete code of your ajax "success" handler. What you've posted here does not look wrong, so it must be something else. – Pointy Jun 17 '11 at 08:03
  • Try an `alert(data)` and check what type of object you get. May be it's not interpreted as JSON. – DanielB Jun 17 '11 at 08:05

3 Answers3

4

Set the dataType to be json so jQuery will convert the JSON to a JavaScript Object.

Alternatively, use getJSON() or send the application/json mime type.

alex
  • 479,566
  • 201
  • 878
  • 984
1

Either set dataType to json or use var json = JSON.parse(data) to do it manually.

EDIT:

I'm adding this because somebody else suggested eval, don't do this because it gets passed straight into a JSON object without any sanitation first, allowing scripts to get passed leading straight into an XSS vulnerability.

Connor Smith
  • 1,274
  • 7
  • 11
  • Thanks Connor Smith I will be accepting your answer in precisely 6 minutes lol how weird I can't accept an answer when yours has solved the problem. I will not be using eval() dont worry :) Thanks again – Deviland Jun 17 '11 at 08:12
  • No, it uses: `new Function("return " + data))();` if JSON.parse is not available. Source, jQuery source code: https://gist.github.com/277432 – Connor Smith Jun 17 '11 at 08:30
  • @Connor I deleted it after I realised you didn't use `$.parseJSON()` (I was imagining things). BTW, passing a string to `new Function()` is basically an `eval()`. – alex Jun 17 '11 at 08:35
  • @alex It seems that it is indeed as bad security-wise but that new Function() is better, smarter, at parsing the data and is faster: http://stackoverflow.com/questions/2449220/jquery-uses-new-functionreturn-data-instead-of-evaldata-to-parse-j – Connor Smith Jun 17 '11 at 08:39
0

The data is the Json so you will want to do this:

success: function (data) {
  var newobject = eval(data);
  alert(newobject.msg);

}

or do this:

$ajax({
    url: url,
    data: {},
   dataType: "json",
   success: function (newObject) {
   alert(newobject.msg);
}
});
Richard
  • 21,728
  • 13
  • 62
  • 101