CORS jQuery AJAX请求 [英] CORS jQuery AJAX request

查看:785
本文介绍了CORS jQuery AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ajax请求的麻烦。我收到错误

I'm having troubles with an ajax request. I was getting the error

在所请求的资源上没有Access-Control-Allow-Origin标头。

所以我试过的是这个 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.

如何解决这个问题?

推荐答案

应该设置服务器http响应头。它与您的前端JavaScript代码没有任何关系。您需要返回此标题:

It's easy, you should set server http response header first. It's no business 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屋!

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