无法从angularjs我自己的API获取数据JSONP

无法从angularjs我自己的API获取数据JSONP

本文介绍了无法从angularjs我自己的API获取数据JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我自己的API获取的数据,但无法在获取同样以vegdata变量成功...这里是控制器code

  $ scope.filterText = NULL;
$ scope.vegdata = [];
$ scope.init =功能(){
URL =?HTTP://192.168.1.17 \\:5000 /回调= JSON_CALLBACK;
$ http.jsonp(URL).success(功能(数据){
        的console.log(数据);
        $ scope.vegdata =数据;
    })错误(功能(errord){
      警报(errord);
    });

JSON格式类似于下......同样,我能够通过浏览器获取

  [{名:Tuar,形象:0.0,胖:0.0,别名:[{vegname:Tuar ,语言:印地文},{vegname:冲克,语言:英语},{vegname:木豆,语言:植物学名}] ,蛋白质:0.0,节能:0.0,碳水化合物:0.0}]


解决方案

您的反应应该是这样的:

  yourCallBack([{名:Tuar,形象:0.0,胖:0.0,别名:[{vegname: Tuar,语言:印地文},{vegname:冲克,语言:英语},{vegname:木豆,语言:植物学名 }],蛋白质:0.0,节能:0.0,碳水化合物:0.0}]);

使用 yourCallBack 回调查询字符串被检索。我不知道如何与蟒蛇做到这一点,在asp.net mvc的我会以这样的:

 字符串回调=的Request.QueryString [回调];

I am trying to fetch data from my own api but was not able to succeed in fetching the same to vegdata variable... here is the controller code

    $scope.filterText = null;
$scope.vegdata =[];
$scope.init = function() {
url = "http://192.168.1.17\:5000/?callback=JSON_CALLBACK";
$http.jsonp(url).success(function(data){
        console.log(data);
        $scope.vegdata = data;


    }).error(function(errord){
      alert(errord);
    });

JSOn format looks like below... the same I was able to fetch through browser

[{"name": "Tuar", "image": "0.0", "fat": "0.0", "alias": [{"vegname": "Tuar ", "language": "Hindi"}, {"vegname": "Red Gram", "language": "English"}, {"vegname": "Cajanus cajan", "language": "Botanical Name"}], "protein": "0.0", "energy": "0.0", "carbohydrates": "0.0"}]
解决方案

Your response should be like this:

yourCallBack([{"name": "Tuar", "image": "0.0", "fat": "0.0", "alias": [{"vegname": "Tuar ", "language": "Hindi"}, {"vegname": "Red Gram", "language": "English"}, {"vegname": "Cajanus cajan", "language": "Botanical Name"}], "protein": "0.0", "energy": "0.0", "carbohydrates": "0.0"}]);

With yourCallBack being retrieved from callback query string. I don't know how to do it with python, in asp.net mvc I would to something like this:

string callback = Request.QueryString["callback"];

这篇关于无法从angularjs我自己的API获取数据JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:42