发送错误消息的WebMethod

发送错误消息的WebMethod

本文介绍了发送错误消息的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到的WebMethod错误信息,我想启动的错误。另外,我想不同的场合不同的错误信息。

  $。阿贾克斯({
            键入:POST,
            网址:betting.aspx / submitFunction
            数据:JSON.stringify({结果:jsonObj,编辑:$(#ctl00_ContentPlaceHolder1_CreateOrEdit)VAL()}),
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON,
            成功:函数(ARG){//调用全成
                了window.location =bHistory.aspx;
            },
            错误:函数(XHR){
                警报(错误!您需要登录,然后重试);
                了window.location =login.aspx的;
                //发生了错误
            }
        });
 

解决方案

我所面临的问题,以及。问题是,如果你把你从Web方法例外,它被包裹着,像这样消息:{消息},堆栈跟踪:在...

我落得这样做创建这个js函数来解析返回的异常。
这是pretty的丑,我很想看看如果任何人拿出更好的东西。

 函数getErrorMessage(五){
    返回e.responseText.substring(e.responseText.indexOf(\消息\:\)+\消息\:\,长度,e.responseText.indexOf(\,\栈));
}
 

How do I get error message from webmethod, I want to initiate error. Also, I want different error messages for different occasions.

        $.ajax({
            type: "POST",
            url: "betting.aspx/submitFunction",
            data: JSON.stringify({ results: jsonObj, edit: $("#ctl00_ContentPlaceHolder1_CreateOrEdit").val() }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(arg) { //call successfull
                window.location = "bHistory.aspx";
            },
            error: function(xhr) {
                alert("Error! You need to login, and try again");
                window.location = "login.aspx";
                //error occurred
            }
        });
解决方案

I've faced that problem as well. the issue is that if you throw an exception from your web method, it gets wrapped, like so "Message":"{your message}","StackTrace":" at ...

what I ended up doing was creating this js function to parse the returned exception.
it's pretty ugly, and I'd love to see if anyone's come up with anything better.

function getErrorMessage(e) {
    return e.responseText.substring(e.responseText.indexOf("\"Message\":\"") + "\"Message\":\"".length, e.responseText.indexOf("\",\"Stack"));
}

这篇关于发送错误消息的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:40