[英]Parse JSON from URL
I am building a web site and I use a url which returns a JSON response, like: 我正在构建一个网站,我使用一个返回JSON响应的URL,如:
{name:mark; status:ok}
I would like to obtain the name, using only JavaScript or jQuery in my HTML page. 我想在我的HTML页面中仅使用JavaScript或jQuery获取名称。
Can somebody help me to do this? 有人可以帮我这样做吗?
Have a look at jQuery's .getJSON()
method, which will make it really easy for you. 看看jQuery的
.getJSON()
方法,这将使你很容易。
$.getJSON('yourURL.php', function(data) {
alert(data.name);
});
If you need more flexibility you should have a look at .ajax()
instead. 如果你需要更多的灵活性,你应该看看
.ajax()
。 .getJSON()
is really just a short hand for the .ajax()
method, suitable for making simple requests to fetch JSON. .getJSON()
实际上只是.ajax()
方法的简写,适合于提取获取JSON的简单请求。 With .ajax()
you will have a lot of more options - specifying an error handler for instance, and much more. 使用
.ajax()
您将拥有更多选项 - 例如指定错误处理程序等等。
$(document).ready(function () {
var url = 'https://graph.facebook.com/me';
$.getJSON(url, function (data) {
alert(data.name);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.