javascript - How can I throw an exception out of an AJAX call and out of a function? -
if have jquery ajax call within function:
function callpagemethod(methodname, parameters) { var pagepath = window.location.href; $.ajax({ type: "post", url: pagepath + "/" + methodname, contenttype: "application/json; charset=utf-8", data: parameters, datatype: "json", success: function (response) { alert("ajax successful!"); }, error: function (response) { // line not working! throw response.responsetext; } }); } // end of function
...i getting error in visual studio 2010:
microsoft jscript runtime error: exception thrown , not caught
.
seems if problem not related visual studio though, particular javascript.
example, can declare variable in function before $.ajax
call, assign in error:
, , throw
out of function after $.ajax
call no problems...
how can throw error out of nested function in way? want catch
error outside of function if possible.
so, here possible solutions have found:
1 -- assign variable error
value, , throw after $.ajax
call:
function callpagemethod(methodname, parameters) { var errorvalue = null; $.ajax({ ... ... ... error: function (response) { errorvalue = response.responsetext; } }); if (errorvalue != null) { throw errorvalue; } }
2 -- make $.ajax
call synchronous:
function callpagemethod(methodname, parameters) { var errorvalue = null; $.ajax({ ... ... ... async: false, error: function (response) { // works: throw response.responsetext; } }); }
Comments
Post a Comment