2015-07-19 53 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