REST API - 找不到404 DELETE方法

时间:2013-07-03 06:25:35

标签: jquery ajax node.js rest

客户端:

 function deleteData()
    {
        var txtId = $("#txtId").val();
        jQuery.ajax({
            url: "http://localhost:8090/delete/"+txtId, 
            type: "DELETE",
            success: function (data, textStatus, jqXHR) { 
                console.log(data); 
            }
        });
    }

服务器端:

var allowCrossDomain = function(req, res, next)
   {
     res.header('Access-Control-Allow-Origin', '*');
     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
     res.header('Access-Control-Allow-Headers', 'Content-Type');
     next();
   }

app.delete('/delete/:id', function (req, res)
 {
    var id = req.params.id;
    userdbConnection.query("DELETE FROM USER WHERE user_id = '"+id+"'", function(err, rows,  fields){});
    res.send("Deleted"+''+id);
 });

输入:

 `txtId = 26`

输出:

删除在DB中执行的操作,我也得到了服务器到客户端的响应。但是我也遇到了OPTIONS http://localhost:8090/delete/26 404 (Not Found)

的错误

这意味着什么?

1 个答案:

答案 0 :(得分:1)

此代码可以帮助我:

var allowCrossDomain = function(req, res, next) 
{
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  if( req.method.toLowerCase() === "options" )
      {
        res.send( 200 );
      }
  else
      {
    next();
      }
}

Thanks for this question