从URL获取JSON文件,XMLHttpRequest无法加载错误 [英] Get a JSON file from URL, XMLHttpRequest cannot load error

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

问题描述

我需要从URL读取JSON文件并显示。
我已阅读了很多帖子,但仍无法解决问题。

I need to read a JSON file from URL and display. I have read a lot of posts but still couldn't fix the problem.

url: http://webapp.armadealo.com/home.json

我遇到这个错误:XMLHttpRequest无法加载

I face this error : XMLHttpRequest cannot load

代码低于

$.getJSON("http://webapp.armadealo.com/home.json", function(data){
alert(data);
});

我试过添加到网址

&callback=?

并使其成为jsonp,仍然没有运气。我还使用了

and making it a jsonp, still no luck. I have also used

<meta http-equiv="Access-Control-Allow-Origin" content="*" />

仍然没有运气。

有吗我们需要在服务器端做什么?
遇到此类错误并找到解决方案的人请帮帮我!
非常感谢!

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!

推荐答案

由于安全原因,您无法制作类似的跨域AJAX请求。因此,如果要从其他域加载内容,则必须使用解决方法:JSONP(更多信息示例

You cannot make cross-domain AJAX requests like that due to security reasons. So if you want to load content from another domain, you will have to use a workaround: JSONP (more info, example)

对AJAX请求使用以下代码:

Use the following code for the AJAX request:

$.ajax({
    url: 'http://webapp.armadealo.com/home.json',
    type: 'GET',
    jsonpCallback: 'myCallback',
    dataType: "jsonp",
    success: function(data) {
        console.log(data);
    }
});

为了使其正常工作,您必须将JSON数据包装在括号中并添加回调开头的名字:

In order for this to work, you will have to wrap the JSON data in parentheses and add the callback name at the beginning:

myCallback({ ... JSON ... })



编辑:刚刚注意到你已经尝试过使用JSONP ......好吧,至少以上代码对我有用,也许你想尝试一下。 ;)


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. ;)

这篇关于从URL获取JSON文件,XMLHttpRequest无法加载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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