簡體   English   中英

asp.net mvc .ajax錯誤捕獲

[英]asp.net mvc .ajax error catching

我有一些問題從控制器通過.ajax錯誤捕獲錯誤消息。 該腳本由jQuery UI Dialog彈出窗口調用。 任何幫助,將不勝感激。

這是我的控制器的代碼片段

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Content("There was an error in your request.", MediaTypeNames.Text.Plain);

這是jQuery腳本:

function submitFormWithAjax(form) {
    form = $(form);
    $.ajax({
        url: form.attr('action'),
        data: form.serialize(),
        type: (form.attr('method')),
        dataType: 'text',
        error: function(data) {
            $('#result').html(data);
        },
        success: function(data) {
            $('#result').html(data);
            setTimeout("reloadPage()", 500);
        }
    });
  return false;
}

error: function(data)未讀取返回的錯誤消息。 有什么建議?

謝謝大家的幫助。 我在經過一些試驗后想出來了,我需要做的就是在jQuery中

    error: function(xhr, textStatus, exceptionThrown) {
        $('#result').html(xhr.responseText);
    },

這在控制器中

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Content("There was an error in your request.");

錯誤回調的簽名是function(xmlHttpRequest, textStatus, exceptionThrown)

也許你正在查看錯誤的參數?

試試這個:

    function submitFormWithAjax(form) {
    form = $(form);
    $.ajax({
        url: form.attr('action'),
        data: form.serialize(),
        type: (form.attr('method')),
        dataType: 'text',
        error: function(data) {
            $('#result').html(data);
        },
        success: function(xmlHttpRequest, textStatus, exceptionThrown) {
            $('#result').html(xmlHttpRequest.statusText);
            setTimeout("reloadPage()", 500);
        }
    });
  return false;
}

它必須是返回BadRequest(錯誤400)。當返回代碼是沖突時,錯誤回調似乎被調用(錯誤409)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM