2016-09-16 62 views
0

以下是我的ajax調用,它工作正常但始終成功。未能正常工作ASP.NET MVC Ajax JQuery

$.ajax({ 
    type: "POST", 
    url: '@(Request.RawUrl + "/postcomment")', 
    dataType: "json", 
    data: data, 
    contentType: "application/json; charset=utf-8", 
    success: function() { 
     alert('success'); 
    }, 
    failure: function(response) { 
     alert(response.d); 
    } 
    }); 

如何使失效功能激活。在控制器類,我做

return new HttpStatusCodeResult(500, "Error message"); 

但在失敗的消息框不會出現,我需要什麼做的就是失敗的工作並顯示一條消息。

+1

這'error'不'failure'。 –

回答

0

使用狀態碼回調。你從哪裏得到失敗回調?回調只有一個error

$.ajax({ 
    statusCode: { 
    500: function() { 
     alert("error"); 
    } 
    } 
}); 
+0

不記得我爲什麼使用失敗,一定在互聯網上的某個地方看過它,只是複製它。 – MiscellaneousUser

2

可以請你error替換failure

error: function(response) { 
    alert(response.d); 
} 
+0

我在某處看到失敗並開始使用它。我已經用我應該使用的正確屬性更新了答案。 statusText是正確的屬性。接受編輯,我會標記答案。 – MiscellaneousUser