0

客户端:

 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`

输出:

在数据库中执行的删除操作,我也得到了服务器到客户端的响应。但我也有一个错误OPTIONS http://localhost:8090/delete/26 404 (Not Found)

这是什么意思?

4

1 回答 1

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();
      }
}

感谢这个问题

于 2013-07-03T08:56:51.093 回答