如何发出jsonp请求 [英] How to make a jsonp request
问题描述
我需要做一些跨站点脚本。下面的代码块包含jsonp的方法,该方法返回就好像失败了,但当我将其更改为get请求时,我就获得了成功。我需要能够使用jsonp方法成功响应。可以排除以下内容。响应是有效的json,这个param在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
以上内容有效,现在正在执行成功方法。我只需要弄清楚如何解析对象。一旦我找到答案,我会再次发帖。
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.
推荐答案
我决定详细说明如何做一个< a href =http://en.wikipedia.org/wiki/JSONP =nofollow noreferrer> 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;
}
<请注意,网址包含?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
param,回调
。这是一个很好的教程。 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屋!