我正在尝试向本地服务器中运行的本地json文件发出发布请求,但是即使指定了所有标头参数,我仍然无法获得状态405方法。我使用的是vscode中的实时服务器扩展,并且我已经尝试过更改端口,但问题尚未解决。我不知道这是我的本地服务器有问题还是我的代码错误。
let book= new Book(title,author,year);
let url = "../data/books.json";
let init =
{
method:"POST",
credentials:"include",
mode:"cors",
headers:
{
"Accept":"application/json, text/plain, */*",
"Content-type":"application/json",
"Authorization":"*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Request-Method": "*",
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Headers":"Origin, Content-type, Accept, Authorization, Credentials"
},
body:JSON.stringify(book)
}
fetch(url,init)
.then(function(res)
{
if(res.status == 200)
return res.json();
else
throw new Error("Request Error");
})
.then(function(obj)
{
console.log(obj);
})
.catch(function(err)
{
alert(err.message);
})