【问题标题】:AngularJS is caching JSONP by defaultAngularJS 默认缓存 JSONP
【发布时间】:2013-04-04 02:52:05
【问题描述】:

我是 AngularJS 的新手,我正在编写一个应用程序,它通过 $http jsonp 方法接收带有回调的 JSON 数据。未设置缓存参数,但浏览器缓存数据。我该如何解决?

$scope.fetch=function () {
$http({method: 'JSONP', url: 'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero'})
.success(function(data) {
$scope.current =  data.salutation.toLowerCase();
console.log($scope.current);
$scope.dim=$scope.current; doIt();}).error(function(data) { 
$scope.current =   data.salutation.toLowerCase() || "Request failed";   $scope.dim=$scope.current;});

};

【问题讨论】:

    标签: javascript angularjs jsonp


    【解决方案1】:

    不确定 Angular 到底在做什么,但这通常是通过将时间戳附加到 url 来完成的。更改您的原始网址:

    'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero'
    

    收件人:

    'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero&_=' + (new Date().getTime())
    

    【讨论】:

    • 这就是我写的代码.. $scope.fetch=function () { $http({method: 'JSONP', url: 'angularjs.org/…'}).success(function(data) { $scope.current = data.salutation.toLowerCase(); console.log($scope.current); $scope.dim=$scope.current; doIt();}).error(function(data) { $scope .current = data.salutation.toLowerCase() || "请求失败"; $scope.dim=$scope.current;}); };
    • 想把它放在你的问题中,以便它可以正确格式化吗?
    • 我改了问题...还是不明白为什么启用了缓存?
    • 因为 jsonp 实际上是页面中的一个 javascript 标签,其中包含您想要加载的任何内容的源代码。除非被告知不要这样做,否则浏览器会缓存 javascript。 Travis 所说的是将一个虚拟参数附加到带有时间戳的 url。因此,当进行新调用时,浏览器会将 url 视为不同的 javascript 并相应地加载它
    • Angular 中的缓存选项是基于 JavaScript 的缓存选项。您遇到了浏览器实际缓存请求的问题。确保您的代码每次都能获得新结果的唯一方法是在每个请求的 URL 中附加一些唯一的内容,即时间戳。我会用更准确的回复编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多