我试图通过连接到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) 我使用Node/Express创建了一个小API并尝试使用Angularjs来提取数据,但是当我的html页面在localhost:8888上运行apache并且节点API在端口3000上侦听时,我得到了No'Access-Control-允许来源.我尝试使用node-http-proxy和Vhosts Apache但没有取得多大成功,请参阅下面的完整错误和代码.
"XMLHttpRequest无法加载localhost:3000.请求的资源上没有'Access-Control-Allow-Origin'标头.因此不允许来自"localhost:8888"."
// Api Using Node/Express
var express = require('express');
var app = express();
var contractors = [
{
"id": "1",
"name": "Joe Blogg",
"Weeks": 3,
"Photo": "1.png"
}
];
app.use(express.bodyParser());
app.get('/', function(req, res) {
res.json(contractors);
});
app.listen(process.env.PORT || 3000);
console.log('Server is running on Port 3000')
Run Code Online (Sandbox Code Playgroud) 我有这个调用函数的动作:
dispatch(Api({url: "my_url", method: "POST", data: data}))
Run Code Online (Sandbox Code Playgroud)
这里我将数组作为数据传递..
import fetch from 'isomorphic-fetch'
export default function Api({url, method, headers, data}={}){
return dispatch => {
console.log(data)
console.log(url)
console.log(method)
console.log(JSON.stringify(data))
let response = fetch(url, {
mode: 'no-cors',
method: method || null,
body: data || null,
}).then(function(response) {
console.log("response");
console.log(response)
});
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我用fetch用mode:'no-cors',我想我想过去的所有arguements ..在这里,我的身体是我作为传递简单arguement阵列..
当我看到响应时,它就像:
body: null
bodyUsed: false
headers: Headers
ok: false
status: 0
statusText: ""
type: "opaque"
url:""
Run Code Online (Sandbox Code Playgroud)
在这里我的身体没被使用..
这里有什么问题?需要帮忙