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

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

问题描述

我有烦恼有一个Ajax请求。我正在错误

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

否访问控制 - 允许 - 原产地标头的请求的资源present。

所以,我想是这样的的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 code无业务。您需要返回这个头:

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的配置文件,在code是这样的:

In Apache config files, the code is like this:

Header set Access-Control-Allow-Origin "*"

在nodejs中,code是这样的:

In nodejs,the code is like this:

res.setHeader('Access-Control-Allow-Origin','*');

这篇关于CORS jQuery的AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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