javascript - 使用Vue的axios vue-resource跨域不成功 但原生xhr就可以
黄舟
黄舟 2017-04-11 13:16:50
[JavaScript讨论组]

使用Vue的Ajax组建axios vue-resource跨域都不成功

但原生xhr就能跨域成功?

xhr请求:

使用axios的请求:

错误提示(axios的):

XMLHttpRequest cannot load http://ss.net/auth. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://vue.js:8080' is therefore not allowed access.

createError.js?f777:15 Uncaught (in promise) Error: Network Error
    at createError (eval at  (build.js:899), :15:15)
    at XMLHttpRequest.handleError (eval at  (build.js:878), :87:14)

code:

    export default {
        data() {
            return {
                mes: '',
            }
        },
        methods: {
            logIn: function (mes) {
                //axios
                this.axios({
                    method: 'post',
                    url: 'http://ss.net/auth',
                    data: {
                        firstName: 'Fred',
                        lastName: 'Flintstone'
                    },
                    withCredentials: false
                });

                //xhr
                var request = new XMLHttpRequest();
                request.open('POST', 'http://ss.net/auth', true);

                request.onload = function() {
                    if (this.status >= 200 && this.status < 400) {

                    }
                };

                request.send();

            },
        },
    }

后端加了一个中间件,测试应该是没问题的:

        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Credentials', 'false')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS')
            ->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Origin, Accept');

后端已经设置了CROS,一天了没整明白,有知道的吗..

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
ringa_lee

我今天才折腾过这个 实际上cors请求会发送两个 第一个是options请求 你看看能不能正确响应并且带有cors相关的首部

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号