[英]How to make a jsonp request
I need to do some cross site scripting. 我需要做一些跨站点脚本。 The block of code below contains the method of jsonp, the method returns as if it failed, but when I change it to be a get request I then have success.
下面的代码块包含jsonp的方法,该方法返回就好像失败了,但当我将其更改为get请求时,我就获得了成功。 I need to be able to a successful response using the jsonp method.
我需要能够使用jsonp方法成功响应。 The following can be ruled out.
可以排除以下内容。 The response is valid json and this param is in the url ?callback=JSON_CALLBACK.
响应是有效的json,这个param在url?callback = JSON_CALLBACK中。 Here is the json I receive from doing the http request and the code block that executes this code.
这是我通过执行http请求获得的json以及执行此代码的代码块。
http response status code 200 http响应状态码200
[{"cube":"1" ,"points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"}]
code block 代码块
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;
}
}
Note that I have edited my server side code and now receive 请注意,我已编辑了我的服务器端代码,现在已收到
"angular.callbacks._1( {"cube":"1","points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"})"
UPDATE The above is valid and now the success method is executing. 更新以上内容有效,现在正在执行成功方法。 I just need to figure out how to parse the objects.
我只需要弄清楚如何解析对象。 I will post again once I figure out the answer.
一旦我找到答案,我会再次发帖。
I have decided to give a detailed description of how to do a jsonp request so others will not run into the same troubles as I did. 我已经决定详细描述如何执行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;
}
Notice that the url contains ?callback=JSON_CALLBACK
. 请注意,url包含
?callback=JSON_CALLBACK
。 Here is a nice stackoverflow on that. 这是一个很好的stackoverflow。 Once you get the response then you'll receive a json like the one below.
一旦你收到回复,你就会得到一个类似下面的json。
"angular.callbacks._1( {"cube":"1","points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"})"
Here is a nice stackoverflow on that subject 这是关于该主题的一个很好的stackoverflow
Now the one part that got me is that the server has to return the GET
param, callback
. 现在给我的一部分是服务器必须返回
GET
参数, callback
。 Here is a good tutorial for that. 这是一个很好的教程。 http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/ So the json looks like the one above.
http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/所以json看起来像上面的那个。
Well, I hope this helps someone in the future. 好吧,我希望这有助于将来的某些人。
If you want to make several JSONP requests through $http service, you should use a little hack. 如果你想通过$ http服务发出几个JSONP请求,你应该使用一点hack。 Agular change JSON_CALLBACK to internal value, and best way to use next solution: put this js code into your returned js-file:
Agular将JSON_CALLBACK更改为内部值,以及使用下一个解决方案的最佳方法:将此js代码放入返回的js文件中:
var callbackId = '_' + (angular.callbacks.counter - 1).toString(36);
angular.callbacks[callbackId](/* YOUR JSON */);
To be sure that this code will work for you, please check createHttpBackend method in angular sources. 为确保此代码适合您,请检查角度源中的createHttpBackend方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.