1

即使将 ajax 与 JSONP 一起使用,是否有任何方法可以强制缓存?

请看以下三个案例和结果:

#1。来自同一主机的请求(保存为发出请求的页面)并且没有 JSONP - 缓存正常工作。

$.ajax({

    type: "GET",
    url: "http://samehost.com",
    data: "{ 'format': 'json' }",
    contentType: "application/json",
    success: function(data) {
        // do stuff with data
    }

});

#2。来自没有 JSONP 的不同主机的请求,我收到same origin policy错误消息。

$.ajax({

    type: "GET",
    url: "http://differenthost.com",
    data: "{ 'format': 'json' }",
    contentType: "application/json",
    success: function(data) {
        // do stuff with data
    }

});

#3。来自不同主机的请求和 JSONP 缓存似乎不起作用 - 需要2-3 sec加载,以防万一#1 about 70-100ms

$.ajax({

    type: "GET",
    url: "http://differenthost.com",
    data: "{ 'format': 'json' }",
    contentType: "application/json",
    dataType: "jsonp",
    success: function(data) {
        // do stuff with data
    }

});

重复我的问题,即使将 ajax 与 JSONP 一起使用,是否有任何方法可以强制缓存?


下面你可以看到我的 ServiceStack 设置。

Globas.asax.vb

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Public Class HelloAppHost
        Inherits AppHostBase

        Public Sub New()
            MyBase.New("Web Services", GetType(Wrapper).Assembly)
        End Sub

        Public Overrides Sub Configure(ByVal container As Container)

            ' Routes
            Routes.Add(Of GetData)("/GetData").Add(Of GetData)("/GetData/{*}")

            ' Cache & Cors 
            container.Register(Of ICacheClient)(New MemoryCacheClient())
            Plugins.Add(New Cors.CorsFeature())


        End Sub

    End Class


    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

        Dim apphost = New HelloAppHost()
        apphost.Init()

    End Sub

End Class

WS.vb

Public Class Wrapper

    <EnableCors()>
    Public Class WrapperGetData
        Inherits Service

        Implements IService(Of GetData)
        Public Function Execute(ByVal request As GetData) As Object Implements ServiceStack.ServiceHost.IService(Of GetData).Execute

        Dim cacheKey As String = "GetDataKey"
        Dim expireInTimespan = New TimeSpan(1, 0, 0)

        Return Me.RequestContext.ToOptimizedResultUsingCache(
          MyBase.Cache,
          cacheKey,
          expireInTimespan,
          Function()
            Return request.HandleRequest()
          End Function
        )

        End Function

    End Class
End Class
4

0 回答 0