从URL解析JSON [英] Parse JSON from URL

查看:133
本文介绍了从URL解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,并且使用一个返回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?

推荐答案

看看jQuery的 方法,这对您来说真的很容易.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆