简体   繁体   English

使用 Ajax 向 http 请求添加 CORS 标头

[英]Add CORS header to an http request using Ajax

I have developed a Restfull application and I'd like to add another web application to consume its services so I make this Ajax call :我开发了一个 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);
            }

        });

I get this exception :我得到这个例外:

Object {readyState: 0, status: 0, statusText: "SyntaxError: Failed to execute 'setRequestHeader' …-Origin: ' is not a valid HTTP header field name."} error DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Access-Control-Allow-Origin: ' is not a valid HTTP header field name.对象 {readyState: 0, status: 0, statusText: "SyntaxError: Failed to execute 'setRequestHeader' …-Origin: ' is not a valid HTTP header field name."} error DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest' : 'Access-Control-Allow-Origin: ' 不是有效的 HTTP 标头字段名称。

So I need to know :所以我需要知道:

  • How can I enable the CORS in this situation?在这种情况下如何启用 CORS?

  • How can I fix my code?我该如何修复我的代码?

You can't authorize yourself like that.你不能这样授权自己。 It's a response header;这是一个响应头; details in the specification .规范中的详细信息。 The server you're sending the request to has to send that header back to let the browser know it's okay to allow your page to send an ajax request to that server.您向其发送请求的服务器必须将该标头发送回,以便让浏览器知道允许您的页面向该服务器发送 ajax 请求是可以的。 There's nothing you can do in your client-side code if the server you're trying to request from doesn't allow your origin.如果您尝试请求的服务器不允许您的源,则您无法在客户端代码中执行任何操作。

somehow i redirected to this question to get the solution for my Flask application.不知何故,我重定向到这个问题以获得我的 Flask 应用程序的解决方案。 Since the server has to send the response back to the header, the CORS has to set in the server side.由于服务器必须将响应发送回标头,因此必须在服务器端设置 CORS。 In my case i was trying to send the request from就我而言,我试图从

client http://127.0.0.1:8081/客户端http://127.0.0.1:8081/

to

server http://127.0.0.1:5051服务器http://127.0.0.1:5051

So i set the cors policy to allow the origin in the client side所以我设置了 cors 策略以允许源在客户端

headers: {  "Access-Control-Allow-Origin:": "*"},

and for the server side, flask provides library to allow the cors对于服务器端,flask 提供了允许 cors 的库

from flask_cors import CORS
app = Flask(__name__)

CORS(app)

it actually got resolved the issue它实际上解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM