2013-07-03 94 views
0

客戶端:REST API - 404沒有找到DELETE方法

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

試試這個答案http://stackoverflow.com/questions/13218174/cors-put-not-found-with-jquery-ajax – user568109

+0

@ user568109謝謝 – Hulk1991

回答

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