【问题标题】:Angularjs $http get method for JSONP data without CALLBACK parameter [duplicate]Angularjs $http获取没有CALLBACK参数的JSONP数据的方法[重复]
【发布时间】:2017-12-15 19:35:36
【问题描述】:

我在表格中打印一些服务器上的 JSON 时遇到问题。 这是我的 JSON

process([
{
    "name": "A",
    "value": "41"
},
{
    "name": "B",
    "value": "71"
},
{
    "name": "C",
    "value": "20"
}],"2017.07.11 15:48:33");

我的控制器:

myApp.controller('liveTable', function ($scope, $http) {

$http.get('http://something.com/get.php?jsonp=2017')
    .then(function (response) {
        $scope.myData= response.data;
        console.log(response.data);
    });

这是我的 HTML

 <div class="liveTable" ng-controller="liveTable">
             <table>
                <tr ng-repeat="item in myData.process">
                    <td>{{item.name}}</td>
                    <td>{{item.value}}</td>
                </tr>
            </table>

        </div>

知道我哪里错了吗? tnx

【问题讨论】:

  • 您在控制台中看到数据了吗?
  • 这似乎不是有效的 json
  • 是的,我在控制台中看到了数据

标签: php angularjs json jsonp http-method


【解决方案1】:

我认为你的 json 数据有问题。

我可以通过更改您的 json 数据来查看。你可以看看。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<body>

<div ng-app="myapp">
 <div class="liveTable" ng-controller="myctrl">
             <table>
                <tr ng-repeat="item in process">
                    <td>{{item.name}}</td>
                    <td>{{item.value}}</td>
                </tr>
            </table>

        </div>
</div>
</div>
<script>
angular.module("myapp",[]).controller("myctrl", function($scope){
  $scope.process = ([
{
    "name": "A",
    "value": "41"
},
{
    "name": "B",
    "value": "71"
},
{
    "name": "C",
    "value": "20"
}]);
 

});
</script>

</body>
</html>

【讨论】:

    【解决方案2】:

    试试:

    app.service("dangerousAPI", function($q) {
      this.get = get;
    
      function get(funcName, url) {
        var dataDefer = $q.defer();
    
        window[funcName] = function(x) {
          dataDefer.resolve(x);
        }
    
        var tag = document.createElement("script");
        tag.src = url;
    
        document.getElementsByTagName("head")[0].appendChild(tag);
    
        return dataDefer.promise;
      }
    })
    
    dangerousAPI.get('process',url).then(function(data) {
         $scope.myData= data;
         console.log(data);
    })
    

    欲了解更多信息,请参阅Not a Legal JSONP API


    当我输入{{myData}}时,我已经完成了jsonp,

    很高兴您能够通过此答案获得数据。它表明服务器没有返回合法的 JSON。应该修复服务器以返回合法的 JSON 或合法的JSONP

    要使服务器以有效的JSONP 数组响应,请将 JSON 包裹在方括号 () 中并在回调之前添加:

    echo $_GET['callback']."([{'fullname' : 'Jeff Hansen'}])";
    Using json_encode() will convert a native PHP array into JSON:
    
    $array = array(
        'fullname' => 'Jeff Hansen',
        'address' => 'somewhere no.3'
    );
    echo $_GET['callback']."(".json_encode($array).")";
    

    欲了解更多信息,请参阅Simple PHP and JSONP example

    【讨论】:

    • 当我放 {{myData}} 时,我有完整的 jsonp,我想逐个元素地打印,有什么想法吗?
    • 很高兴您能够通过此答案获得数据。它表明服务器没有返回合法的 JSON。服务器应修复为返回合法的 JSON 或合法的 JSONP
    猜你喜欢
    • 2017-11-28
    • 2020-02-07
    • 1970-01-01
    • 2015-05-03
    • 2018-09-25
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多