简体   繁体   中英

Session state with jQuery/Ajax and ASP.NET web service

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM