views:

32

answers:

0

Hi,

I am calling the webservice with jQuery Ajax method. Now I am looking for a way to pass the server side response to the client . The response will not be so simple in the client side in "success" method(of $.ajax) I need to notify the client what is the status of the desired task which he made through his request, I need to notify the client with a custom message from the server (like: you can perform this operation), I need to throw the data too from the server(normal JSON data ). Till now I am with a thought of making a DTO for it, which will be sent from the server to the client as response. Below is the code for that DTO

[Serializable]
public class AjaxResponse
{
    public string ResponseStatus { get; set; }// success or failure
    public string ResponseMessage { get; set; } //Any Message which we need to show
    public object ResponseData { get; set; } //any data 
}

The client will call the webservice for a particular method:

[WebMethod]
public AjaxResponse foo(int param1,string param2)
{
//Server side processing then return a AjaxResponse
}

Details of AjaxResponse properties:

ResponseStatus: It will be a kind of text such as "success" or "failure", it will tell the client whether the task for which the request was made is succeeded or not. Like if in the request the input params are not valid the "ResponseStatus" will be "failed" else "succeeded" (after the server side task has been done).

ResponseMessage: It will be any message which I want to show the client for what reason the desired task can not be accomplished or if it is succeeded then show the client a custom message from server side.

ResponseData: It will be any data string, int, bool, DTO, Collection of objects etc from the server side which will be sent to client which he requested.

Can please help me in making such kind of feature ? Is my approach correct or if something better is needed can you please suggest it ?