2016-09-16 103 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