views:

382

answers:

3

Hi,

I'm trying to consume an ASP.NET web service, using jQuery's Ajax methods. I want the return type of the web service method to be JSON-formatted data, and I'm marking the method with these attributes:

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public MyObject[] GetMyObjects(int pageIndex) {/*...*/}

I want to also return from the method some sort of value that would indicate whether the session has expired, either in the SOAP headers an output parameter. How would I do this with jQuery syntax? Should I wrap my MyObject[] array in a serializable ResponseType object that contains a SessionValid property and the payload? Or is it possible to use SOAP headers and output parameters with jQuery?

Thanks.

+1  A: 

Should I wrap my MyObject[] array in a serializable ResponseType object that contains a SessionValid property and the payload?

This is the way I usually go with.
Not time consuming to implement and very easy to maintain.

[Serializable]
public class MyReturn
{
  public MyObject[] MyObjectList { get; set; }
  public bool SessionExpired { get; set; }
}

Then handle it where you do the AJAX call.

EDIT: I usually only use

//...
contentType: "application/json; charset=utf-8",
dataType: "json", 
//...

in my AJAX calls to be sure the returned type is in JSON format.

Alex Bagnolini
I was thinking the same thing actually, before I posted my response, but I don't see an intuitive way to determine if the HttpContext's current session was created on the current request.
regex
+1  A: 

I'm not completely familiar with using session in SOAP web services. However, I did stumble on this post which states that the JavaScript will need to account for cookies since this is how the session is maintained.

Although, SOAP may use a different method for tracking session via it's headers, so I don't know if this is accurate.

regex
Thanks. But is JScript/Infopath the same as JavaScript as implemented in a web browser?
alord1689
Ok, after testing, it appears that session state is not being retained. I'm going to have to figure out how to pass the cookies in the ajax call.
alord1689
Ok, it's working after all. I just had a couple bugs in my code.
alord1689
A: 

Thanks, with this method "[WebMethod(EnableSession = true)]" i can access to session variables

Elenardo