2012-12-07 3 views
2

여기에 설명 할 수없는 것이 있습니다.C# WebMethod로 HTTP_POST jQuery Ajax 요청 결과 캐싱

클라이언트에서 브라우저에 캐시하려는 큰 JSON 데이터 세트가 있습니다. jQuery AJAX를 사용하여 C# 웹 메서드를 호출하고 있습니다.

[System.Web.Services.WebMethod] 

여기에 jQuery를 없습니다 :

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

모든 설정 :

  $.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 
       } 
      }); 

을 아무리 웹 메소드 내 서버에서 지정한 것, HTTP 헤더는 항상이처럼 보이는 돌아와 나는 webservice에 넣어 아래와 같이 신속하게 무시됩니다 :

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

웹 서비스가 HTTP GET과 호환되지 않습니다. 맞습니까? 또는 어떻게해야합니까?

감사합니다.

답변

1

,이 같은 캐시 응답 헤더를 설정할 수 있습니다

<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 

다음과 같이 GET 아약스 사용하여 웹 서비스를 호출

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); 
    } 
}); 
0

캐시를 설정할 수있는 속성이 WebMethod 속성에 있습니다. 이 헤더에 캐시 값을 설정하면 잘 모르겠습니다. 최대 제어를 위해

http://msdn.microsoft.com/en-us/library/byxd99hx%28v=vs.71%29.aspx#vbtskusingwebmethodattributecacheduration

+0

덕분에, 내가해야 나는 행운을 빌어 WebMethod에서 CacheDuration을 시도했다고 덧붙였다. – kevin

+0

HttpContext.Current.Response.End(); 돌아 오기 전에 방법의 끝에서? –