如何发出 jsonp 请求 [英] How to make a jsonp request
问题描述
我需要做一些跨站点脚本.下面的代码块包含 jsonp 的方法,该方法返回好像失败一样,但是当我将其更改为 get 请求时,我就成功了.我需要能够使用 jsonp 方法成功响应.可以排除以下情况.响应是有效的 json,这个参数在 url 中?callback=JSON_CALLBACK.这是我从执行 http 请求中收到的 json 和执行此代码的代码块.
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. I need to be able to a successful response using the jsonp method. The following can be ruled out. The response is valid json and this param is in the url ?callback=JSON_CALLBACK. Here is the json I receive from doing the http request and the code block that executes this code.
http 响应状态码 200
http response status code 200
[{"cube":"1" ,"points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"}]
代码块
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.
推荐答案
我决定详细说明如何做一个jsonp 请求,这样其他人就不会遇到和我一样的麻烦.
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.
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;
}
注意 url 包含 ?callback=JSON_CALLBACK
.这是一个很好的 stackoverflow. 一旦你得到响应然后你会收到一个像下面这样的json.
Notice that the url contains ?callback=JSON_CALLBACK
. Here is a nice stackoverflow on that. Once you get the response then you'll receive a json like the one below.
"angular.callbacks._1( {"cube":"1","points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"})"
现在让我知道的一个部分是服务器必须返回 GET
参数,callback
.这是一个很好的教程.http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/ 所以 json 看起来像上面的那个.
Now the one part that got me is that the server has to return the GET
param, 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.
好吧,我希望这对未来的人有所帮助.
Well, I hope this helps someone in the future.
这篇关于如何发出 jsonp 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!