【发布时间】:2020-05-12 17:10:20
【问题描述】:
我在 pythonanywhere 上使用 react-native 作为前端,使用 django 作为后端。 我的前端代码:
axios({
method: 'POST',
url: 'http://mohammadss.pythonanywhere.com/login',
// url: '127.0.0.1:8000/login',
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
},
data: qs.stringify({
Hello: 'Hello?',
}),
})
.then(res => {
console.log(res.data);
})
.catch(res => {
console.log(res);
});
和我的后端代码:
def Login(request):
res = request.POST ;
return JsonResponse(res , JSONEncoder)
但它不会返回任何东西。有了邮递员,一切都很好。并且使用 fetch() 函数我可以看到结果:
fetch('http://mohammadss.pythonanywhere.com/login/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: qs.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
}).then(reponse => {
reponse.json().then(RES => {
console.log(RES);
});
});
但是 axios 没用。
【问题讨论】:
-
console.log 中出现什么错误?
-
您可能面临
CORS错误。你的CORS是否在你的 django 设置中正确配置?
标签: django react-native axios fetch