axios post dosent将任何数据发送到django
我在pythonywhere上使用react native作为前端,使用django作为后端。 我的前端代码:axios post dosent将任何数据发送到django,django,react-native,axios,fetch,Django,React Native,Axios,Fetch,我在pythonywhere上使用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;c
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不起作用 我找到了自己的路。我所做的事情: 1-已在我的Pythonywhere主机上安装]。多亏了尼玛。(当然,我不知道这是否必要!) 2-我将代码更改为:
const data = new FormData();
data.append('username', 'myname');
data.append('password', 'blablabla');
axios
.post('http://mohammadss.pythonanywhere.com/login/', data, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(function(response) {
console.log(typeof response.data.Code);
if (response.data.Code == '1') {
alert('Loged IN');
}
})
.catch(function(error) {
console.log(error);
});
我不知道有多少我的代码是干净的,但为我工作 您在console.log中遇到了什么错误?您可能面临
CORS
错误。您的CORS
是否在django的设置中正确配置?