jQuery技术:从webmethod发送错误消息

如何从webmethod获取错误消息,我想发起错误。 另外,我想要针对不同场合使用不同的错误消息。

$.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 } }); 

    我也遇到过这个问题。 问题是,如果你从你的web方法中抛出exception,它会被包装,就像"Message":"{your message}","StackTrace":" at ...

    我最终做的是创建这个js函数来解析返回的exception。
    这很丑陋,我很想知道是否有人能想出更好的东西。

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

    你可以试试这个:

    需要了解更多jQuery教程分享从webmethod发送错误消息,都可以关注jQuery技术分享栏目—计算机技术网(www.ctvol.com)!

     error: function(msg, status) { if (status === "error") { var errorMessage = $.parseJSON(msg.responseText); alert(errorMessage.Message); } } 

      以上就是jQuery教程分享从webmethod发送错误消息相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/982246.html

      (0)
      上一篇 2021年12月13日
      下一篇 2021年12月13日

      精彩推荐