Javascript处理JSON [英] Javascript handling JSON
问题描述
这似乎很奇怪...
我有一些JSON ...
I have some JSON...
{"firstName":"David","lastName":"Smith","email":null,"id":0}
但是当我尝试解析它并将其与...一起使用时
But when I try to parse it and use it with...
<script>
$(document).ready(function() {
var json = $.getJSON('userManagement/getUser');
$("p").text(json.firstName);
});
</script>
This is the user management view
Users : <p></p>
什么都没有出现,但是如果我只是做$("p").text(json);它告诉我这是一个对象,我可以在Firebug中看到JSON是正确的,有什么主意吗?
Nothing appears, but if I just do $("p").text(json); it tells me it's an object and I can see the JSON is correct in firebug, any ideas?
推荐答案
尝试:
<script>
$(document).ready(function() {
$.getJSON('userManagement/getUser',function(json){
$("p").text(json.firstName);
});
});
</script>
在完成AJAX请求后,您必须使用json
变量.
You have to work with the json
variable after the AJAX request has completed.
在此处了解有关AJAX JSON请求的更多信息: http://api.jquery.com/jQuery.getJSON /
Learn more here about AJAX JSON Requests: http://api.jquery.com/jQuery.getJSON/
在此处了解有关常规AJAX请求的更多信息: http://api.jquery.com/jQuery.ajax /
Learn more here about general AJAX Requests: http://api.jquery.com/jQuery.ajax/
这篇关于Javascript处理JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!