如果不使用香草js,我总是使用jQuery进行AJAX
请求。现在,由于React已经接手了,要发出AJAX
请求,就不需要使用整个jQuery
库发出这些请求,因此,我们鼓励使用内置fetch
方法的js ,axios或其他方法。
我一直在尝试POST
使用发送请求fetch
。我能够使用axis
但不能提取。
axios.post('https://reqres.in/api/login', {
"email": "peter@klaven",
"password": "cityslicka"
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios代码看起来像这样,但是当我尝试使用相同的东西时,我认为fetch
它是行不通的。谁能看到我想念的东西?值已发布,但API返回错误,因此我必须做错了什么。
var data = {
"email": "peter@klaven",
"password": "cityslicka"
}
fetch("https://reqres.in/api/login", {
method: "POST",
body: JSON.stringify(data)
})
.then(function(response){
return response.json();
})
.then(function(data){
console.log(data)
});
var headers = {
"Content-Type": "application/json",
"Access-Control-Origin": "*"
}
尝试将上述行添加到标题中。
var data = {
"email": "peter@klaven",
"password": "cityslicka"
}
fetch("https://reqres.in/api/login", {
method: "POST",
headers: headers,
body: JSON.stringify(data)
})
.then(function(response){
return response.json();
})
.then(function(data){
console.log(data)
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句