我试图通过连接到Flask内置的RESTful API来使用JavaScript进行授权.但是,当我发出请求时,我收到以下错误:
XMLHttpRequest无法加载http:// myApiUrl/login.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.
我知道API或远程资源必须设置标题,但为什么在我通过Chrome扩展程序Postman发出请求时它可以正常工作?
这是请求代码:
$.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);
});
Run Code Online (Sandbox Code Playgroud) 我已经开发了一个有角度的网站,并在proxy.conf.js文件中有以下代理设置.
const proxyConfig = [
{
context: '/web/api/webclients/**',
target: 'https://10.109.102.109',
changeOrigin: true,
secure: false
},
{
context: '/restful/**',
target: 'https://10.109.110.190',
changeOrigin: true,
secure: false
},
{
context: '/api/**',
target: 'http://10.109.105.107',
changeOrigin: true,
secure: false
}
];
Run Code Online (Sandbox Code Playgroud)
在开发模式下,proxy.conf.js按预期工作.
我在package.json文件中有这些用于启动和构建.
"build": "ng build --aot --prod",
"start": "ng serve --proxy-config proxy.conf.js -o",
Run Code Online (Sandbox Code Playgroud)
运行"npm run build"并使用生成的文件在IIS 8上托管网站后,需要使用代理设置的页面不起作用.
例如,我的请求https:// localhost/web/api/webclients/authentication应该转到https://10.109.102.109/web/api/webclients/authentication
如果我在Windows服务器上托管此网站,如何在IIS中设置这些代理设置?如果我在非Windows服务器上托管它,如何设置这些代理设置?