繁体   English   中英

从POSTMAN成功后,通过Axios的发布请求失败。 怎么样?

[英]Post request via Axios fails while it succeeds from POSTMAN . How?

我正在尝试使用axios向ubuntu vps(django后端)上的端点执行发布请求。 该api响应在成功的发布请求之后创建的201。 使用Postman,我能够执行成功的发布请求,但是当我使用Axios尝试时,我可以看到控制台中捕获了异常(我使用过console.log)。

这是我通过Axios进行的操作:

.
.
.


axios.defaults.xsrfCookieName = 'csrftoken'
axios.defaults.xsrfHeaderName = 'X-CSRFToken'

// Tried without headers as well
var headers = {'Content-Type': 'application/x-www-form-urlencoded'}

axios.post('http://127.0.0.1:8000/api/v1/subscribe/',data_str,headers)
.then(function(response) {
console.log('saved successfully');
// this.isHidden = false;
alert("Subscription succesful !!");
console.log(response)
}).catch((error) =>{
if(error.response){
console.log('1..........Response Error');
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
}
else if(error.request){
// This part gets printed in the browser console. No idea why
console.log('2..........Request Error');
console.log(error.request);
}
else{
console.log('3..........Other error');
console.log('Error', error.message);
}
console.log(error.config);
} );
var headers = {'Content-Type': 'application/x-www-form-urlencoded'}
axios.post('http://127.0.0.1:8000/api/v1/subscribe/',data_str,headers)
.then(function(response) {
console.log('saved successfully');
// this.isHidden = false;
alert("Subscription succesful !!");
console.log(response)
}).catch((error) =>{
if(error.response){
console.log('1..........Response Error');
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
}
else if(error.request){
console.log('2..........Request Error');
console.log(error.request);
}
else{
console.log('3..........Other error');
console.log('Error', error.message);
}
console.log(error.config);
} );

**我的Django rest api中的CORS配置很少:

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_CREDENTIALS = True

CORS_ALLOW_METHODS = ['DELETE','GET','OPTIONS','PATCH','POST','PUT',]

CORS_ALLOW_HEADERS = ['*']

这是控制台中错误的图片这是浏览器中的控制台日志

如果有人帮助我,我深表感谢。

提前致谢 !

感谢您的投入。 我解决了这个问题。 执行POST请求的URL是http://localhost:8000/api/v1/subscribe ,替换为http://my website.com/api/v1/subscribe后,该URL成功运行。

我在执行实际的URL:

也就是说, http://my website.com/api/v1/subscribe在Postman中,这就是它成功的原因。 正如我所知道的那样,我正在使用本地主机,但始终出错。 ☺️

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM