jQuery.getJSON - 访问控制允许来源问题 [英] jQuery.getJSON - Access-Control-Allow-Origin Issue
问题描述
我正在使用 jQuery 的 $.getJSON()
函数来返回一组简短的 JSON 数据.
I'm jusing jQuery's $.getJSON()
function to return a short set of JSON data.
我将 JSON 数据放在一个 url 上,例如 example.com
.我没有意识到,但是当我访问同一个 url 时,无法加载 JSON 数据.我通过控制台发现 XMLHttpRequest
由于 Access-Control-Allow-Origin
无法加载.
I've got the JSON data sitting on a url such as example.com
.
I didn't realize it, but as I was accessing that same url, the JSON data couldn't be loaded. I followed through the console and found that XMLHttpRequest
couldn't load due to Access-Control-Allow-Origin
.
现在,我已经阅读了很多网站,它们只是说使用 $.getJSON()
并且这将是解决方法,但显然它不起作用.是否应该在标题或函数中更改某些内容?
Now, I've read through, a lot of sites that just said to use $.getJSON()
and that would be the work around, but obviously it didn't work. Is there something I should change in the headers or in the function?
非常感谢您的帮助.
推荐答案
很简单,使用 $.getJSON()
函数,在你的 URL 中只包含
It's simple, use $.getJSON()
function and in your URL just include
回调=?
作为参数.这会将调用转换为 JSONP,这是进行跨域调用所必需的.更多信息:http://api.jquery.com/jQuery.getJSON/
as a parameter. That will convert the call to JSONP which is necessary to make cross-domain calls. More info: http://api.jquery.com/jQuery.getJSON/
这篇关于jQuery.getJSON - 访问控制允许来源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!