AJAX /错误块将其作为错误请求捕获? [英] AJAX / error block captuing it as a bad request?

查看:83
本文介绍了AJAX /错误块将其作为错误请求捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



请您帮忙解决以下问题?



我们回来了JQUERY调用中抛出JSON对象的异常?



以下是我们如何在控制器中重新调整异常。



Hello,

Would you please help me with the below issue?

We are returning the exception throw JSON object in the JQUERY call?

Below is how we are retuning the exception in the controller.

protected JsonResult ThrowJSONError(Exception e)
{
    return Json(new { Message = e.Message }, JsonRequestBehavior.AllowGet);
}







以下是我们捕获异常的方式:



var getSubDecisions = function(p){

$ .blockUI();

var url = window.location.pathname + / ABC / XYZ;

url = url.replace('//','/');

$ .ajax({

url:url,

数据:{

decisionId:$(p).val()

},

类型:POST,

数据类型:json,

完成:function(){

$ .unblockUI();

},

成功:功能(结果){



},

错误:函数(xhr,状态,错误){

handleServerException(xhr);

}

});

};



假设上述AJAX中存在异常。那么它会出错:(块),然后






below is how we capturing the exception:

var getSubDecisions = function(p) {
$.blockUI();
var url = window.location.pathname + "/ABC/XYZ";
url = url.replace('//', '/');
$.ajax({
url: url,
data: {
decisionId: $(p).val()
},
type: "POST",
datatype: "json",
complete: function () {
$.unblockUI();
},
success: function (result) {

},
error: function (xhr, status, error) {
handleServerException(xhr);
}
});
};

lets assume that there is an exception in the above AJAX. then it would go the error: (block) , and then

// Genernal Purpose function to handle/display exceptions
var handleServerException = function (xhr) {
    var errors = [];
    //var errorMessage = $.parseJSON(xhr.responseText);
    errors.push(xhr.responseText);
    showErrors(errors, "DecisionForm");
};

// Show validation errors in overlay
var showErrors = function (errors, formName) {
    // Clear errors from Validation Summary control
    clearValidationSummaryForm(formName);



    var currentForm = $('form[name="' + formName + '"]');

    // Add error messages to the list
    $.each(errors, function (index, value) {
        //message parsing
        var errorMessage = value;
        currentForm.find('div[data-valmsg-summary] ul').append('<li>' + errorMessage + '</li>');
    });

    // Change the css style from non-error to error state
    currentForm.find('div[data-valmsg-summary]')
        .removeClass('validation-summary-valid')
        .addClass('validation-summary-errors');

    // Show the div overlay that is used to display the errors
    currentForm.find('div.validationSummary').show();
};







如上所示,当有例外情况时AJAX调用,它会将Error变为黑色,从那里它会去handleServerException函数,从那里它将转到showErrors()...在show errors函数中我们只是试图将消息推送到UI ...它适用于页面验证错误,但是当它是一个AJAX错误时...它抛出了错误的请求错误而不是我们传递的异常......但是当我们通过visual studio测试时它工作正常它也可以正常工作在已部署的服务器上测试应用程序但是..在服务器之外它抛出(推送)错误请求而不是我们想要看到的异常...



请让我知道你对此问题有任何疑问。



感谢你的帮助。




As you can see above, when there is an exception in the AJAX call , it would go the Error black , from there it would to go handleServerException function and from there it would go to showErrors()... in the show errors function we are just trying to push the message to the UI... it works fine for the page validation erros but when it is an AJAX error.. it is throwing the bad request error instead of the exception we passed... but it works fine when we test through visual studio and it also works fine if we test application on the deployed server but.. outside the server it is throwing (pushing) bad request instead of the exception we are suppose to see..

Please let me know if you have any question about the issue.

Thanks for your help in advance.

推荐答案

.blockUI();

var url = window.location.pathname +/ ABC / XYZ;

ur l = url.replace('//','/');
.blockUI();
var url = window.location.pathname + "/ABC/XYZ";
url = url.replace('//', '/');


.ajax({

url:url,
数据:{

decisionId:
.ajax({
url: url,
data: {
decisionId:


(p).val()

},

类型:POST,

数据类型:json,

完成:function(){
(p).val()
},
type: "POST",
datatype: "json",
complete: function () {


这篇关于AJAX /错误块将其作为错误请求捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆