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.