如果不使用香草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);
});
Run Code Online (Sandbox Code Playgroud)
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)
});
Run Code Online (Sandbox Code Playgroud)
var headers = {
"Content-Type": "application/json",
"Access-Control-Origin": "*"
}
Run Code Online (Sandbox Code Playgroud)
尝试将上述行添加到标题中。
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)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8179 次 |
| 最近记录: |