[英]Get a JSON file from URL, XMLHttpRequest cannot load error
I need to read a JSON file from URL and display. 我需要从URL读取JSON文件并显示。 I have read a lot of posts but still couldn't fix the problem. 我已阅读了很多帖子,但仍无法解决问题。
url : http://webapp.armadealo.com/home.json 网址: http : //webapp.armadealo.com/home.json
I face this error : XMLHttpRequest cannot load 我遇到这个错误:XMLHttpRequest无法加载
The code is below 代码如下
$.getJSON("http://webapp.armadealo.com/home.json", function(data){
alert(data);
});
I have tried adding to the url 我试过添加到网址
&callback=?
and making it a jsonp, still no luck. 并使它成为一个jsonp,仍然没有运气。 I have also used 我也用过
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
still no luck. 仍然没有运气。
Is there anything that we need to do at the server side? 我们需要在服务器端做些什么吗? People who have faced such an error and have found a solution, Help me out please! 遇到这种错误并找到解决方案的人请帮帮我! Thanks a lot! 非常感谢!
You cannot make cross-domain AJAX requests like that due to security reasons. 由于安全原因,您无法制作类似的跨域AJAX请求。 So if you want to load content from another domain, you will have to use a workaround: JSONP ( more info , example ) 因此,如果要从其他域加载内容,则必须使用解决方法:JSONP( 更多信息 , 示例 )
Use the following code for the AJAX request: 对AJAX请求使用以下代码:
$.ajax({
url: 'http://webapp.armadealo.com/home.json',
type: 'GET',
jsonpCallback: 'myCallback',
dataType: "jsonp",
success: function(data) {
console.log(data);
}
});
In order for this to work, you will have to wrap the JSON data in parentheses and add the callback name at the beginning: 为了使其工作,您必须将JSON数据包装在括号中并在开头添加回调名称:
myCallback({ ... JSON ... })
EDIT: Just noticed you already tried to use JSONP... Well, at least the above code works for me, perhaps you want to give it a try. 编辑:刚刚注意到你已经尝试过使用JSONP ......好吧,至少上面的代码对我有用,也许你想尝试一下。 ;) ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.