使用获取发出POST请求

用户名

如果不使用香草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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章