在reactJs中调用axios的问题

问题描述 投票:0回答:2

我是ReactJ的新手,我需要使用axios调用从数据库中获取值,但是它显示404错误这是我的代码

axios({
   method: 'POST',
   contentType: 'application/json; charset=utf-8',
   url:'http://localhost/projectName/backend/web/category-master/fetch-category',
   responseType: 'json'
    })
php reactjs yii2
2个回答
1
投票

问题是缺少本地主机后的端口号。请设置端口号,然后重试

axios({
     method: 'POST',
     contentType: 'application/json; charset=utf-8',
     url:'http://localhost:[PORT]/wellnesscc/backend/web/category-master/fetch-category',
     responseType: 'json'
 })

例如:

如果您的后端应用程序的端口是5000,则正确的版本如下:

axios({  
         method: 'POST',
         contentType: 'application/json; charset=utf-8',
         url:'http://localhost:5000/wellnesscc/backend/web/category-master/fetch-category',
         responseType: 'json'
     })

0
投票

尝试以下操作,首先检查您的url路由方法是否接受发布方法。

axios.post(url,data, {
headers: {
    'authorization': your_token_if_any,
    'Accept' : 'application/json',
    'Content-Type': 'application/json'
}
})
.then(response => {
     // return  response;
})
.catch((error) => {
    //return  error;
});
© www.soinside.com 2019 - 2024. All rights reserved.