admin管理员组

文章数量:1336632

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

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

Share Improve this question edited Jun 17, 2011 at 8:07 Deviland asked Jun 17, 2011 at 8:02 DevilandDeviland 3,3747 gold badges34 silver badges53 bronze badges 2
  • 1 It would help a lot if you'd post the plete code of your ajax "success" handler. What you've posted here does not look wrong, so it must be something else. – Pointy Commented Jun 17, 2011 at 8:03
  • Try an alert(data) and check what type of object you get. May be it's not interpreted as JSON. – DanielB Commented Jun 17, 2011 at 8:05
Add a ment  | 

3 Answers 3

Reset to default 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.

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.

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);
}
});

本文标签: javascriptJquery JSON response handlingStack Overflow