c# - Handling errors raised by jQuery Ajax method -


i have page in using jquery ajax method call simple webservice data database , bind controls in page. ajax method called on onchange event of select (here http request post). following jquery ajax method

function callajax(url, jsondata, sucessfunction, failurfunction) { $.ajax({     type: "post",     url: url,     data: jsondata,     contenttype: "application/json; charset=utf-8",     datatype: "json",     success: sucessfunction,     error: function(){        alert('error occured');     } }); 

}

the url above method somepage.aspx/getdatafromdatabse getdatafromdatabse webservice method. our testers testing page using burp suite. when directly tying access url(www.example.com/somepage.aspx/getdatafromdatabse) in browser, http method shown in burp suite , error raised , user getting redirected appropriate page. when directly accessing above url , intercepting request in burp suite , changing request post request following error message displayed directly in browser:

{"message":"there error processing request.","stacktrace":"","exceptiontype":""}

the "error" in above ajax function not getting executed , alert box not shown , able handle error. how handle such error , redirect user custom page?

it appear response server valid json response, despite fact contains (to you) reads error. such $.ajax(...) call handling response success, not error.

you can return valid json response , within response indicate whether there error or not, along additional info such user-friendly error message, redirect url, etc.

the error: handler of $.ajax(...) call should used real server response errors (i.e. unhandled 500 errors, timeouts etc.)

for example, successful json response like:

{     success: true,     errormessage: null,     errorredirecturl: null,     data: { .... successful data response .... } } 

and failed json response (e.g. due validation problems, not server failures) this:

{     success: false,     errormessage: 'there error processing request.',     errorredirecturl: 'http://....someurl...',     data: null } 

then check response.success == true in success: option of $.ajax(....) call, , handle appropriately.

by having single, consistent structure success: option handles ajax requests complete , determines whether there handled error , it, such display alert user, redirect them url, etc...

in contrast, error: option handles ajax requests didn't come response can use.

update:

on server handling difference between receiving get http request , post http request. so, think problem need detect whether post request contains valid data in format webservice method needs.

so, web service needs decide if posted data in http request in format expects receive , if not return http redirect response (302) rather returning error.

you in 2 ways:

  1. using try... catch... block. webservice method appears throwing exception , returning it, catch , instead of returning it, set , return 302 response instead.

  2. if genuine calls webservice can generate exceptions under normal operations (this bad!) you'll need validate data received in request see if webservice method expects receive (was data received? right structure sent ajax request? etc.)

note: if request does contain valid data there no easy way detect whether came genuine source (the ajax call) or forged request other means. there are solutions this, involved , can require multiple calls authentication etc.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -