Search code examples
c#web-servicesjqueryhttp-caching

Caching an HTTP_POST jQuery Ajax Request Result with C# WebMethod


Here's something I can't figure out.

I've got the large JSON data set that I'd like the client to cache in browser. I'm using jquery AJAX to call a c# web method.

[System.Web.Services.WebMethod]

Here's the jQuery:

            $.ajax({
                url: "/Ajax/ItemAffinity.aspx/FetchAffinityItems?ItemID=" + escape($("#SearchSelectedPageID").val()),
                type: "POST",
                data: "{}",
                cache: true,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                //contentType: "application/json; charset=utf-8",
                success: function (data) {
                   //do cool stuff
                }
            });

No matter what I specify on the server within the web method, the HTTP HEADERS always come back looking like this:

Cache-Control   no-cache
Content-Length  919527
Content-Type    application/json; charset=utf-8
Expires -1

Any settings I put into the webservice are promptly ignored, like the below:

        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(1));
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);

Webservices don't work with HTTP GET's, right? Or how should I do this?

Thanks!


Solution

  • For maximum control, you can set the cache response headers like this:

        <WebMethod()> _
        <ScriptMethod(UseHttpGet:=True)> _
        Public Function get_time() As String
    
            'Cache the reponse to the client for 60 seconds:
            Dim context As HttpContext = HttpContext.Current
            Dim cacheExpires As New TimeSpan(0, 0, 0, 60)
            SetResponseHeaders(context, HttpCacheability.Public, cacheExpires)
    
            Return Date.Now.ToString
    
        End Function
    
        ''' <summary>
        ''' Sets the headers of the current http context response.
        ''' </summary>
        ''' <param name="context">A reference to the current context.</param>
        ''' <param name="cacheability">The cachability setting for the output.</param>
        ''' <param name="delta">The amount of time to cache the output. Set to Nothing if NoCache.</param>
        ''' <remarks></remarks>
        Public Shared Sub SetResponseHeaders(ByRef context As HttpContext,
                                             cacheability As HttpCacheability,
                                             delta As TimeSpan)
    
            If Not IsNothing(context) Then
                Dim cache As HttpCachePolicy = context.ApplicationInstance.Response.Cache
                cache.SetCacheability(cacheability)
    
                Select Case cacheability
                    Case HttpCacheability.NoCache
                        'prevent caching:
                        Exit Select
                    Case Else
                        'set cache expiry:
                        Dim dateExpires As Date = Date.UtcNow
                        dateExpires = dateExpires.AddMinutes(delta.TotalMinutes)
                        'set expiry date:
                        cache.SetExpires(dateExpires)
                        Dim maxAgeField As Reflection.FieldInfo = cache.GetType.GetField("_maxAge", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
                        If Not IsNothing(maxAgeField) Then
                            maxAgeField.SetValue(cache, delta)
                        End If
                End Select
            End If
    
        End Sub
    

    Then call your webservice using ajax GET like this:

    var postObj = {
        ItemID: 12
    }
    
    $.ajax({
        url: webserviceUrl,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: postObj,
        success: function (reponse) {
            alert(response.d);
        }
    });