(AXIOS GET)
我请求.net core web api 2.1
时使用的是本机响应,但是在控制台上看到的错误如下:
注意: Cors是由.net core 2.1
__授予的。
代码:
return axios.get('http://127.0.0.1:50000/api/values', {
credentials: 'include'
})
.then(
(response) => {
console.log(response);
}
)
.catch(
(error) => {
console.log(error);
}
);
错误:
Error: Network Error
at createError (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\axios\lib\core\createError.js:16)
at XMLHttpRequest.handleError (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\axios\lib\adapters\xhr.js:87)
at XMLHttpRequest.dispatchEvent (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\event-target-shim\lib\event-target.js:172)
at XMLHttpRequest.setReadyState (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:580)
at XMLHttpRequest.__didCompleteResponse (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:394)
at D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:507
at RCTDeviceEventEmitter.emit (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190)
at MessageQueue.__callFunction (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:349)
at D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:106
at MessageQueue.__guard (D:\Projeler\Mobile\ReactNative\primCepte\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:297)
发布于 2019-04-09 09:08:15
只是遇到了同样的问题。
即使您没有得到CORS错误,您可能仍然缺少Access-Control-Allow-Origin
头,它仍然“错误”您的请求。
您需要添加CORS到您的ASP.net核心应用程序。
services.AddCors(options =>
options.AddPolicy("MyPolicy",
builder => {
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
}
)
);
然后
app.UseCors("MyPolicy");
在此之前
app.UseMvc();
这很方便,并将允许任何 ajax请求到您的API,但这并不理想。
您可以允许IP和您选择的域。如何在在ASP.NET核心中启用跨源请求(CORS)中阅读更多内容
https://stackoverflow.com/questions/53746034
复制相似问题