相关疑难解决方法(0)

如何发出jsonp请求

我需要做一些跨站点脚本.下面的代码块包含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)

更新 以上内容有效,现在正在执行成功方法.我只需要弄清楚如何解析对象.一旦我找到答案,我会再次发帖.

javascript json angularjs

15
推荐指数
1
解决办法
4万
查看次数

标签 统计

angularjs ×1

javascript ×1

json ×1