通过jQuery AJAX从远程URL检索JSON数据 [英] Retrieve JSON data from remote URL via jQuery AJAX
问题描述
我正在使用以下代码从URL获取数据.
I am using following code to get the data from URL.
$.ajax({
url: 'http://183.77.251.173:90/api/function/getprice.aspx?code=1301&length=3M',
success: function (data) {
alert(data.results[0].address_components[0].long_name);
},
error: function (jqXHR, exception) {
alert(jqXHR.status);
}
但是它将引发错误,状态代码为0
.我不知道这是什么原因?我也尝试设置了crossDomian:true
,但是它仍然抛出相同的错误.
However it throws an error with status code of 0
. I don't know what the reason is for this? I tried to set crossDomian:true
also but it still throws same error.
我还将URL修改为http://www.google.com
,该URL还返回了错误状态代码0
.为什么?是什么原因?从远程URL获取数据的正确方法是什么?
I also modified the URL to http://www.google.com
which also returns the error status code of 0
. Why? What is the reason? What is the correct way to get the data from a remote URL?
推荐答案
除非使用JSONP或CORS,否则您无法发出跨域请求.这些资源的可用性将取决于您从中请求信息的域的API.
You cannot make a cross domain request unless you're using JSONP or CORS. The availability of those will depend on the API of the domain you are requesting information from.
这是现代浏览器的一项安全功能,称为相同起源政策.
This is a security feature of modern browsers known as the Same Origin Policy.
这篇关于通过jQuery AJAX从远程URL检索JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!