使用 AJAX 解析有效的 JSON?
嗨,我仍在学习这个 JSON 的东西...
我想我找到了一个有效的 JSON:
http: //www.nfl.com/liveupdate/scorestrip/ss.json
问题是我不确定如何使用 AJAX 解析它?
这可以吗?我也听说过MooTools 有什么区别?
另外,这里还有一些我只是想玩一下但似乎不起作用的代码:
$.ajax({
type: "GET",
url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
dataType: "json",
success: function(data) {
// Interpret response
for (var i = 0; i < data.gms.length; i++) {
document.write("Day: " + data.gms[i][0]);
document.write("<br/>");
document.write("Time: " + data.gms[i][1]);
document.write("<br/><br/>");
}
}
});
Hi I am still learning this JSON stuff...
I think I found a valid JSON:
http://www.nfl.com/liveupdate/scorestrip/ss.json
Problem is I am not sure how I can parse this using AJAX?
Is this possible to do? I have also heard of MooTools what is the difference?
Also here is some code I have just to play around but doesn't seem to work:
$.ajax({
type: "GET",
url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
dataType: "json",
success: function(data) {
// Interpret response
for (var i = 0; i < data.gms.length; i++) {
document.write("Day: " + data.gms[i][0]);
document.write("<br/>");
document.write("Time: " + data.gms[i][1]);
document.write("<br/><br/>");
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSON 代表“JavaScript 对象表示法”。 AJAX 代表“异步 Javascript 和 XML”。 MooTools 是一个跨浏览器 JavaScript 框架,它为开发人员提供了面向对象的 JavaScript 工作流程。
不要将苹果和豌豆混淆。这三种技术经常一起使用,但又是不同的东西。 JSON 是一种描述 javascript 对象、数组和文字的表示法; AJAX 用于请求和检索文档(包括 JSON 格式的文档),MooTools 可用于进行 AJAX 调用(可能检索 JSON 格式的文档 - 或 XML 片段)。
JSON 是纯 JavaScript,不需要以任何方式“解析”它,JavaScript 解释器会为你做这件事。 AJAX 通常用于描述 XMLHttpRequest:源自 javascript 的 http 请求,通常检索 JSON 或 XML/HTML 数据。 MooTools 允许您使用相对较少的代码通过其
Request
类:JSON stands for "JavaScript Object Notation". AJAX stands for "Asynchronous Javascript And XML". MooTools is a crossbrowser javascript framework which provides developers an object oriented workflow with javascript.
Don't confuse apples with peas. All three technologies are often used together, but are different things. JSON is a notation to describe javascript objects, arrays and literals; AJAX is for requesting and retrieving documents (including documents in JSON format) and MooTools can be used to make AJAX calls (probably retrieving a document in JSON format – or an XML fragment).
JSON is pure javascript, there is no need to "parse" it in any way, the javascript interpreter will do that for you. AJAX is often used to describe XMLHttpRequests: http requests originating from javascript, often retrieving JSON or XML/HTML data. MooTools allows you with relatively little code to make use of the XMLHttpRequest feature trough its
Request
class:我猜你的问题是因为你发出了跨域请求,而你的浏览器阻止了该请求。要处理来自另一个域的 json 响应,您需要使用 jsonp 数据类型。它基本上是在 ajax 请求中更改 dataType:"jsonp" 。
在同源策略下,server1.example.com 提供的网页无法正常连接或与除 server1.example.com 以外的服务器通信。 HTML 元素是一个例外。利用元素的开放策略,某些页面使用它们来检索对来自其他来源的动态生成的 JSON 格式数据进行操作的 Javascript 代码。这种使用模式称为 JSONP。
请阅读相关内容。有关 jquery ajax 中的 abt json 的更多信息: http://api.jquery.com/jQuery.ajax/< /a>
你更新的代码:
由于该网站似乎不在你的控制之下,你可以让脚本点击你的网址,即你的本地网站网址,其中你的本地服务器现在联系实际的网址获取json并按原样发送它
I am guessing your problem is cos your making a cross domain request and your browser is blocking that requst. To process a json response from another domain you need to use jsonp datatype. Its basically chaging the dataType:"jsonp" in your ajax request.
Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML element. Taking advantage of the open policy for elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP.
Do read about it. For more info abt json in jquery ajax: http://api.jquery.com/jQuery.ajax/
your updated code:
since the site doesnt seem to be under your control you can have the script hit your url i.e your local site url wherein your local server now contacts the actual url gets the json and sends it as it is
我建议您首先研究http请求,当您请求数据或接收数据时发生了什么。
一旦您学习了基础知识,那么请求和接收数据就会很容易。
HTTP 请求
I would recommend you first study the http requrest, Whats going on when you request data or receive data.
Once you learn the basic, then this will be easy to request and receive data.
HTTP Request