rag*_*ubs 15 javascript json angularjs
我需要做一些跨站点脚本.下面的代码块包含jsonp的方法,该方法返回就好像失败了,但当我将其更改为get请求时,我就获得了成功.我需要能够使用jsonp方法成功响应.可以排除以下内容.响应是有效的json,这个param在url?callback = JSON_CALLBACK中.这是我通过执行http请求获得的json以及执行此代码的代码块.
http响应状态码200
[{"cube":"1" ,"points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"}]
Run Code Online (Sandbox Code Playgroud)
代码块
var myApp = angular.module('test', []);
myApp.controller('UserCtrl', function($scope, users) {
$scope.usersPerCube = users.getUsers();
})
myApp.factory('users', function($http) {
return {
getUsers: function() {
var deferred = $q.defer();
var url = "http://localhost/api/api/index.php/analytics/UsersPerCube?callback=JSON_CALLBACK";
$http.get(url).success(function (data, status, headers, config) {
console.log(data);
deferred.resolve(data);
}).error(function (data, status, headers, config) {
//this always gets called
console.log(status);
deferred.reject(status);
});
return deferred.promise;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我已编辑了我的服务器端代码,现在已收到
"angular.callbacks._1( {"cube":"1","points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"})"
Run Code Online (Sandbox Code Playgroud)
更新 以上内容有效,现在正在执行成功方法.我只需要弄清楚如何解析对象.一旦我找到答案,我会再次发帖.
rag*_*ubs 29
我已经决定详细描述如何执行jsonp请求,以便其他人不会遇到与我相同的麻烦.
myApp.factory('users', function($http) {
return {
getUsers: function() {
var deferred = $q.defer();
var url = "http://localhost/api/api/index.php/analytics/UsersPerCube?callback=JSON_CALLBACK";
$http.get(url).success(function (data, status, headers, config) {
console.log(data);
deferred.resolve(data);
}).error(function (data, status, headers, config) {
//this always gets called
console.log(status);
deferred.reject(status);
});
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
请注意,网址包含?callback=JSON_CALLBACK. 这是一个很好的stackoverflow. 一旦你收到回复,你就会得到一个类似下面的json.
"angular.callbacks._1( {"cube":"1","points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"})"
Run Code Online (Sandbox Code Playgroud)
现在,让我得到的一个部分是服务器必须返回GETparam , callback. 这是一个很好的教程. http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/所以json看起来像上面的那个.
好吧,我希望这有助于将来的某些人.
| 归档时间: |
|
| 查看次数: |
37576 次 |
| 最近记录: |