本文介绍了反应:Axios网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我第一次使用axios,遇到错误.
This is my first time using axios and I have encountered an error.
axios.get(
`http://someurl.com/page1?param1=1¶m2=${param2_id}`
)
.then(function(response) {
alert();
})
.catch(function(error) {
console.log(error);
});
使用正确的url和参数,当我检查网络请求时,确实可以从服务器中获得正确的答案,但是当我打开控制台时,我看到它没有调用回调,而是捕获了错误.
With the right url and parameters, when I check network requests I indeed get the right answer from my server, but when I open console I see that it didn't call the callback, but instead it caught an error.
推荐答案
如果使用NodeJS创建API
您的Express应用程序需要使用CORS(跨源资源共享).将以下内容添加到您的服务器文件中:
If Creating an API Using NodeJS
Your Express app needs to use CORS (Cross-Origin Resource Sharing). Add the following to your server file:
// This should already be declared in your API file
var app = express();
// ADD THIS
var cors = require('cors');
app.use(cors());
要更全面地了解CORS,请阅读有关CORS的Mozilla文档.
For fuller understanding of CORS, please read the Mozilla Documentation on CORS.
这篇关于反应:Axios网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!