【发布时间】:2018-01-23 12:18:17
【问题描述】:
我正在尝试使用 jquery 和 ajax 从 api 端点获取 json 对象。这是我的代码:
$.ajax({
url: 'https://this-specific-website.com/customers.json',
type: 'GET',
contentType: 'application/json'
}).done((response)=> {
console.log('HELLLLLO INSIDE response')
console.log('response', response)
})
我也对$.getJSON 进行了同样的尝试,如下所示:
$.getJSON('https://this-specific-website.com/customers.json', (response)=> {
console.log('HELLO INSIDE response')
console.log('response', response)
})
但我不断收到以下错误:
请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'null'。
有没有办法解决这个问题?
提前谢谢你!!
更新:
我终于让它工作了。我没有在前端使用 jquery 和 ajax,而是创建了一个单独的 .js 文件并像这样使用http.get():
【问题讨论】:
-
您请求 json 的服务器没有启用 CORS。如果他们支持 JSONP,那么就使用它,否则你必须在后端发出请求。
-
或许this answer可以帮忙
-
为了使用 CORS(跨域资源共享)来使用资源,服务器应该授权您的访问。
-
可能是重复的!看看这个stackoverflow.com/questions/43824511/…
标签: javascript jquery json ajax url