how to handle the ajax response data in one place before the other handlers in jquery
how to handle the ajax response data in one place before the other handlers
In my web application,I use a lot of $.ajax method to send request to the server and get the response data,and I also use some jQuery plugin like jsTree,datatables etc.(I use these plugin's ajax data source),
and the app have a permission control system, so if the user haven't login in, the ajax request will get some json reponse data like {havePermission:"no",permisisonName:"PERMISSION_NAME"},so I want to find a place to handle the ajax respone data before all the other handlers.I have tried the .ajaxComplete,.ajaxError,.ajaxSuccess,even extend the $.ajax method, but It all doesn't works.
so is th开发者_运维百科ere some place to achieve this goal?
You such scenarios you can create a base class for all http handlers where you can override ProcessRequest method to do some user validation stuff.
Thus any of you httpHandler's process request will first call base.processrequest and then continue with its own business logic.
In the base class processRequest, if the validation does not passes, you can write the response there only.
Thanks
Pranav Kaushik
Pardon me if I have understood you incorrectly.
Hoping that you want a generic place on client side where all the requests that are going to server and all the response that are coming from server can be trapped and validated. Right?
If that is the case, here is a way using AJAX
write your own wrapper over AJAX call something like this
function doAJAX(reasonToCallServer, url, processData, data, isAync, contentType, callbackmethod){
ValidateRequest(reasonToCallServer, data);
$.ajax({
url: url,
processData: false,
data: data,
success: function(response){ValidateResponse(reasonToCallServer, contentType, response, callbackmethod)
}});
}
Now you can define your ValidateRequest and ValidateResponse methods and do whatever generic validations you want to do.
Notice in ValidateResponse you have recieved the callbackMethod parameter, which is the actual method to be called when all validations are done.
And ResonToCallServer is nothing but a string for the context in which you are calling the server. Thus you can do a switch over this and do your context specific validations too.
Feel free to ask for further clarifications. And vote if you feel this helps :)
Thanks,
Pranav Kaushik
精彩评论