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
3 Answers
Reset to default 4Set 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
版权声明:本文标题:javascript - Jquery JSON response handling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742411721a2469924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论