角$ http.jsonp()方法仅一次 [英] Angular $http.jsonp() method works only once
问题描述
响应是succeded(警示事件>完成),但第二和另一命中将响应错误。
我试图增加一些配置PARAMS与'缓存:假,但仍仅第一次。我应该明确一些缓存/历史或某事?</ P>
$ scope.add2 =功能(){
无功配置= {
// URL:http://www.where2play.pl/index.php/json/getallusers,
缓存:假的,
//类型:POST,
跨域:真正的,
//回调:JSON_CALLBACK,
//数据:D,
的contentType:应用/ JSON
数据类型:JSONP
};
变种R = Math.floor(的Math.random()* 1000)+ 4;
变种D = {user_type_id:0,user_venue_id:0,FNAME:R};
变种E = objToString(D);
//$http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+即配置)
$ http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ E)
.success(函数(RES){
的console.log('RES:'+ res.user);
警报('完成');
})
.error(函数(){
的console.log('错误');
警报('错误');
});
};
这是在<一个新的问题href="http://stackoverflow.com/questions/31496842/jsonp-and-post-action-in-ionic-framework-angular-js">jsonp及后行动离子框架(angular.js)
我已经添加到服务器响应'angular.callbacks._0(JSON数据之前......也许这里的错误?
这是我的问题的解决方案: 现在我dynamiccaly得到回调参数可通过改变(不总是angular.callback_0,但也可以是:angular.callback_1,angular.callback_2等)从GET方法在服务器处,并把它响应数据菲之前在PHP
&LT; PHP的标题(内容类型:应用程序/ JSON;')?;
$ JSON = json_en code($结果);
回声$ _GET ['回调']'('$ JSON。)。
?&GT;
问题是因为URL是越来越缓存在浏览器和其他的时间,将获取从cache.So我建议你添加新的虚拟参数里面你网址
,将有电流 Date.now()
,这样每次调用服务的时间,这将使网址
独特能源部 Date.now()
部分。
code
<$p$p><$c$c>$http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ E +'和虚设='+ Date.now())//&LT; - 这将增加新的价值,每次做网址唯一Response is succeded (alert->done), but second and another hits will response 'error'.
I've tried to add some config params with 'cache: false' but still works only first time. Should I clear some cache/history or sth?
$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');
});
};
This is the new question in jsonp and post action in ionic framework (angular.js)
I've added to server response 'angular.callbacks._0(' before json data... maybe here's mistake?
This is solution for my issue: now i'm dynamiccaly getting the callback parameter which can by vary (not always angular.callback_0, but can be: angular.callback_1, angular.callback_2, etc.) from GET method at a server and put it before response data f.e in php:
<?php header('content-type: application/json;');
$json=json_encode($result);
echo $_GET['callback'].'('.$json.')';
?>
Issue is because url is getting cached in browser and the other time it will fetch from the cache.So I'd suggest you to add new dummy parameter inside you URL
that will have current Date.now()
so that every time you call service that will make URL
unique doe to Date.now()
component.
Code
$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
这篇关于角$ http.jsonp()方法仅一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!