除了CORS谷歌浏览器扩展程序,还有其他替代方法吗?如何在不使用CORS的情况下成功进行Ajax请求? [英] Is there any alternative to CORS google chrome extension? How to make successful ajax request without using CORS?

查看:145
本文介绍了除了CORS谷歌浏览器扩展程序,还有其他替代方法吗?如何在不使用CORS的情况下成功进行Ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了Udemy API,该API使用React和axios获取课程.如果Chrome启用了CORS扩展名,则效果很好,否则无法获取数据.

我已经问过有关此问题的问题,请花点时间阅读.我已经尝试了在线提供的所有解决方案.谢谢

相关问题: 无法加载资源. Access-Control-Allow-Origin不允许起源

解决方案

此问题通常与后端服务器有关,但是如果您无法访问服务器,则有两个选择

第一个选项以使用此chrome扩展程序: https://cors-anywhere.herokuapp.com/http://example.com

http://cors-proxy .htmldriven.com/?url = http://www.htmldriven.com/sample.json

CORS代理是为需要绕过与执行对第三方服务的标准AJAX请求有关的同源策略的开发人员提供的免费服务.

这是使用CORS代理进行Axiox呼叫的示例

const urlProxy = 'https://cors-anywhere.herokuapp.com/http://example.com';
export function post() {
    let users = {
        username: '',
        password: '',
    };
    return axios({
            method:'POST',
            url:urlProxy,
            data: users, // Delete it if you dont have a data
            withCredentials: true, // Delete it if your request doesn't required credentials
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
                'Origin': '*',
                'Access-Control-Allow-Headers': '*',
                'Access-Control-Allow-Origin': '*',
            }
        })

            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            })
}

我添加了withCredentials(),它使您的浏览器在XHR请求中包含cookie和身份验证标头.如果您的服务依赖于任何cookie(包括会话cookie),则只能使用此选项集.

有一个 Firefox 扩展,可将CORS标头添加到在2015年3月5日发布的最新Firefox(内部版本36.0.1)上运行的所有HTTP响应中 查看此链接

希望这对您有帮助

Hi I have build Udemy API that fetches the courses using React and axios. It works fine if the chrome has CORS extension turned on but otherwise it does not fetch the data.

I have already asked a question regarding this issue, please take a moment to read. I have tried all the solutions provided online.. Thanks

Related issue: Failed to load resource. Origin is not allowed by Access-Control-Allow-Origin

解决方案

This problem usually related with the backend server, but if you don't have an access to the server so you have two options

First option to use this chrome extension: Allow-Control-Allow-Origin but unfortunately this extension is not available in the other browsers so you need to use

Second option by using online CORS proxy like

https://cors-anywhere.herokuapp.com/http://example.com

http://cors-proxy.htmldriven.com/?url=http://www.htmldriven.com/sample.json

CORS proxy is a free service for developers who need to bypass same-origin policy related to performing standard AJAX requests to 3rd party services.

Here's an example of Axiox call using CORS proxy

const urlProxy = 'https://cors-anywhere.herokuapp.com/http://example.com';
export function post() {
    let users = {
        username: '',
        password: '',
    };
    return axios({
            method:'POST',
            url:urlProxy,
            data: users, // Delete it if you dont have a data
            withCredentials: true, // Delete it if your request doesn't required credentials
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
                'Origin': '*',
                'Access-Control-Allow-Headers': '*',
                'Access-Control-Allow-Origin': '*',
            }
        })

            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            })
}

I added withCredentials() it makes your browser include cookies and authentication headers in your XHR request. If your service depends on any cookie (including session cookies), it will only work with this option set.

There's a Firefox extension that adds the CORS headers to any HTTP response working on the latest Firefox (build 36.0.1) released March 5, 2015 Check out this link

Hope this will help you

这篇关于除了CORS谷歌浏览器扩展程序,还有其他替代方法吗?如何在不使用CORS的情况下成功进行Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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