首页 文章

使用Ajax将CORS头添加到http请求

提问于
浏览
0

我开发了一个Restfull应用程序,我想添加另一个Web应用程序来使用它的服务,所以我进行了这个Ajax调用:

$.ajax({
            type: "Post",
            async: false,
            url: "ip_adress/Inviter/api/Account/Register",
            data: donne,
            headers: {  "Access-Control-Allow-Origin:": "*"},
            success: function (data) {
                console.log(data);
                var tab = [];
                tab["username"] = username;
                tab["password"] = pwd;
                var isLogged = Login.CheckCredential(tab, username);
                return isLogged;
            },
            error: function (xhr, status, error) {
                console.log(xhr);
                console.log(status);
                console.log(error);
            }

        });

我得到这个例外:

Object {readyState:0,status:0,statusText:“SyntaxError:无法执行'setRequestHeader'... -Origin:'不是有效的HTTP头字段名 . ”}错误DOMException:无法在'XMLHttpRequest上执行'setRequestHeader' ':'Access-Control-Allow-Origin:'不是有效的HTTP头字段名称 .

所以我需要知道:

  • 如何在这种情况下启用CORS?

  • 如何修复我的代码?

1 回答

  • 4

    你可以't authorize yourself like that. It'一个 response Headers ; the specification中的详细信息 . server 你're sending the request to has to send that header back to let the browser know it'可以允许你的页面向该服务器发送ajax请求 . 有's nothing you can do in your client-side code if the server you'试图请求不允许你的起源 .

相关问题