I am trying to do authorization using JavaScript by connecting to the RESTful API built in Flask. However, when I make the request, I get the following error:
我试图通过连接到Flask内置的RESTful API来使用JavaScript进行授权。但是,当我发出请求时,我收到以下错误:
XMLHttpRequest cannot load http://myApiUrl/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
XMLHttpRequest无法加载http:// myApiUrl / login。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许原点'null'访问。
I know that the API or remote resource must set the header, but why did it work when I made the request via the Chrome extension Postman?
我知道API或远程资源必须设置标题,但为什么在我通过Chrome扩展程序Postman发出请求时它可以正常工作?
This is the request code:
这是请求代码:
$.ajax({ type: "POST", dataType: 'text', url: api, username: 'user', password: 'pass', crossDomain : true, xhrFields: { withCredentials: true }}) .done(function( data ) { console.log("done"); }) .fail( function(xhr, textStatus, errorThrown) { alert(xhr.responseText); alert(textStatus); });
38 个解决方案:https://www.itdaan.com/blog/2013/11/17/3282e36c81f229c7fc67344781b5f50e.html