CORS jQuery AJAX 请求 [英] CORS jQuery AJAX request
本文介绍了CORS jQuery AJAX 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在处理 ajax 请求时遇到问题.我收到错误
I'm having troubles with an ajax request. I was getting the error
请求的资源上不存在Access-Control-Allow-Origin"标头.
所以我尝试的是这个 jQuery ajax 请求:
So what I tried was this jQuery ajax request:
var request = $.ajax({
type: 'GET',
url: url,
dataType: "json",
xhrFields: {
withCredentials: true
}
});
request.done(function(data){
console.log(data);
});
但是还是不行.我仍然收到错误.
But it is still not working. I am still getting the error.
我应该如何解决这个问题?
How should I fix this?
推荐答案
很简单,你应该先设置服务器 http 响应头.问题不在于您的前端 JavaScript 代码.您需要返回此标头:
It's easy, you should set server http response header first. The problem is not with your front-end javascript code. You need to return this header:
Access-Control-Allow-Origin:*
或
Access-Control-Allow-Origin:your domain
在Apache配置文件中,代码是这样的:
Header set Access-Control-Allow-Origin "*"
在nodejs中,代码是这样的:
In nodejs,the code is like this:
res.setHeader('Access-Control-Allow-Origin','*');
这篇关于CORS jQuery AJAX 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文