2015-07-19 78 views
1

响应成功(alert-> done),但第二个和另一个命中将响应'错误'。

我试着用'cache:false'添加一些配置参数,但仍然只能第一次使用。我应该清除一些缓存/历史或某事?

$scope.add2 = function() { 

    var config = { 
     //url : 'http://www.where2play.pl/index.php/json/getallusers', 
     cache: false, 
     //type : 'POST', 
     crossdomain: true, 
     //callback: 'JSON_CALLBACK', 
     //data: d, 
     contentType: "application/json", 
     dataType: "jsonp", 
    }; 

      var r = Math.floor(Math.random() * 1000) + 4; 
      var d = {user_type_id:0, user_venue_id:0, fname:r}; 
      var e = objToString(d); 


//$http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ e, config) 

$http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ e) 
       .success(function(res){ 
        console.log('res:' + res.user); 
        alert('done'); 
       }) 
       .error(function(){ 
        console.log('error');  
        alert('error'); 
       }); 
     }; 

这是jsonp and post action in ionic framework (angular.js)

新的问题,我已经添加到服务器响应“angular.callbacks._0(”前JSON数据......也许这里的错误?

这是解决方案对于我的问题: 现在我dynamiccaly获取回调参数,可以通过GET方法在服务器上变化(不总是angular.callback_0,但可以是:angular.callback_1,angular.callback_2等),并将其放在响应数据fe在PHP:

<?php header('content-type: application/json;'); 
$json=json_encode($result); 
echo $_GET['callback'].'('.$json.')'; 
?> 
+0

从那里你打它,你可以提供的代码? –

+0

我正在使用离子frm,这里是sinppets:http://codepen.io/studentttt/pen/BNPPoP – studentttt

回答

2

问题是因为URL在浏览器中获取缓存,其他时间就从cache.So取我建议你添加新的虚拟参数里面你URL,将有电流Date.now()这样每次你打电话服务,将使URL独特的doe到Date.now()组件。

代码

$http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ 
e + '&dummy='+ Date.now()) //<--this will add new value everytime to make url unique 
+0

我已经尝试过这种情况发生相同的,最奇怪的是:每个请求完成与200和repsonse ,但$ http跳转到错误case – studentttt

+0

@studentttt它不应该正确地转到错误 –

+0

,那么为什么它会呢? – studentttt