从URL解析JSON [英] Parse JSON from URL
问题描述
我正在建立一个网站,并且使用一个返回JSON响应的URL,例如:
I am building a web site and I use a url which returns a JSON response, like:
{name:mark; status:ok}
我想在HTML页面中仅使用JavaScript或jQuery来获取名称.
I would like to obtain the name, using only JavaScript or jQuery in my HTML page.
有人可以帮我做到这一点吗?
Can somebody help me to do this?
推荐答案
Have a look at jQuery's .getJSON()
method, which will make it really easy for you.
$.getJSON('yourURL.php', function(data) {
alert(data.name);
});
如果您需要更大的灵活性,则应该看看 .ajax()
. .getJSON()
实际上只是.ajax()
方法的简写形式,适合于进行简单的请求以获取JSON.使用.ajax()
,您将有更多选择-例如指定错误处理程序等等.
If you need more flexibility you should have a look at .ajax()
instead. .getJSON()
is really just a short hand for the .ajax()
method, suitable for making simple requests to fetch JSON. With .ajax()
you will have a lot of more options - specifying an error handler for instance, and much more.
这篇关于从URL解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!