Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS CRUD api与delete方法一起出现问题_Javascript_Json_Node.js_Crud_Http Status Code 400 - Fatal编程技术网

Javascript NodeJS CRUD api与delete方法一起出现问题

Javascript NodeJS CRUD api与delete方法一起出现问题,javascript,json,node.js,crud,http-status-code-400,Javascript,Json,Node.js,Crud,Http Status Code 400,在过去的两天里,我一直在尝试获取我的nodejscrudapi的Delete部分,但我不知道哪里出了问题。我还在学习——一定有一些我没有看到的东西导致它不能正常工作 在服务器端,我的删除路由如下所示: app.delete('/delete lion/:id',(req,res)=>{ //控制台日志('ID',请求正文.ID); var lion=lions.findIndex(lion=>lion.id==req.body.id); 如果(!狮子[狮子]){ res.send(); }否则

在过去的两天里,我一直在尝试获取我的nodejscrudapi的Delete部分,但我不知道哪里出了问题。我还在学习——一定有一些我没有看到的东西导致它不能正常工作

在服务器端,我的删除路由如下所示:

app.delete('/delete lion/:id',(req,res)=>{
//控制台日志('ID',请求正文.ID);
var lion=lions.findIndex(lion=>lion.id==req.body.id);
如果(!狮子[狮子]){
res.send();
}否则{
var deletedLion=狮子[狮子];
狮子座。拼接(狮子座,1);
res.json(deletedLion);
res.send();
}

})
您的服务器正在
'/DELETE lion/:id'
上侦听删除请求。在此路径中,需要
id
参数。您的服务器将响应

fetch('http://localhost:9091/delete-lion/1337'),

但不是

fetch('http://localhost:9091/delete-lion').

要解决问题,您需要从路由中删除
/:id
,或者让您的请求将
lion.id
作为路由参数(由回调中的
req.params.id
引用)而不是请求体中发送。您还可以通过将route参数更改为
'/delete lion/:id?
使其成为可选参数,但这会让人感到困惑——您可能不希望在后续代码希望id参数出现在主体中时建议该参数应该出现在URI中。

您的服务器正在侦听
'/delete lion/:id'
上的删除请求。在此路径中,需要
id
参数。您的服务器将响应

fetch('http://localhost:9091/delete-lion/1337'),

但不是

fetch('http://localhost:9091/delete-lion').

要解决问题,您需要从路由中删除
/:id
,或者让您的请求将
lion.id
作为路由参数(由回调中的
req.params.id
引用)而不是请求体中发送。您还可以通过将route参数更改为
“/delete lion/:id?”
,使其成为可选的,但这会让人感到困惑——当后续代码希望id参数出现在主体中时,您可能不想建议该参数应该出现在URI中。

无需向Fetch API调用传递单独的主体参数, 相反,只需将“id”参数附加到Fetch调用,如下所示:

function deleteData(id){
 let url = 'http://localhost:9091/delete-lion/'+id;
 return fetch(url,{
    method: "delete",
    headers: {
        'Accept': 'application/json',
        'Content-Type' : 'application/json',
    },
    credentials: "include" //No need for body since ID is passed as param in the URL
  })
  ...
  ...
}
在服务器端,您必须替换

var lion = lions.findIndex(lion=>lion.id==req.params.id);//body with params

这应该可以解决它。这也是RESTful的,而不是在主体中传递“id”

无需向Fetch API调用传递单独的主体参数, 相反,只需将“id”参数附加到Fetch调用,如下所示:

function deleteData(id){
 let url = 'http://localhost:9091/delete-lion/'+id;
 return fetch(url,{
    method: "delete",
    headers: {
        'Accept': 'application/json',
        'Content-Type' : 'application/json',
    },
    credentials: "include" //No need for body since ID is passed as param in the URL
  })
  ...
  ...
}
在服务器端,您必须替换

var lion = lions.findIndex(lion=>lion.id==req.params.id);//body with params

这应该可以解决它。这也是RESTful,而不是在主体中传递“id”

这是客户端获取。当响应与本例中的JSON不同时,它将抛出。尝试response.text()并可能在Try catch中抛出该步骤。这是客户端获取。当响应与本例中的JSON不同时,它将抛出。尝试response.text()并可能在Try catch中抛出该步骤http://localhost:9091/delete-lion/'+id--您需要在
delete lion
id
let url>之间加一个斜杠http://localhost:9091/delete-lion/'+id--您需要在
delete lion
id
之间加一条斜线。