Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将http.get(//JSON from server)中的JSON数据分配给Javascript变量?_Javascript_Arrays_Angularjs_Json - Fatal编程技术网

如何将http.get(//JSON from server)中的JSON数据分配给Javascript变量?

如何将http.get(//JSON from server)中的JSON数据分配给Javascript变量?,javascript,arrays,angularjs,json,Javascript,Arrays,Angularjs,Json,我尝试了一切方法,用$http.get(“我的JSON数据的url”).success(函数(响应){}分配我的JSON数据(JSON对象?) 在javascript中添加一个局部变量,但它不起作用。我对所有这些json字符串和对象都感到困惑。下面是我的代码 我从服务器获取的json数据的形式 { "status":"success", "data":{ "user":{ "username":"user1", "fullName":"

我尝试了一切方法,用$http.get(“我的JSON数据的url”).success(函数(响应){}分配我的JSON数据(JSON对象?) 在javascript中添加一个局部变量,但它不起作用。我对所有这些json字符串和对象都感到困惑。下面是我的代码

我从服务器获取的json数据的形式

{
   "status":"success",
   "data":{
      "user":{
         "username":"user1",
         "fullName":"name "       
      },
      "vehicles":[ ],
      "chargeBoxes":
      [
         {
            "id":1,            
            "name":"Station 1",
            "availability":"offline", 
         },
         {
            "id":2,            
            "name":"Station 2",
            "availability":"online", 
         },
         {
            "id":3,            
            "name":"Station 3",
            "availability":"online", 
         }
     ]
}
所以,我的问题是如何使用这些数据并将其存储在js数组中。我的javascript highchart控制器需要它。我尝试了这个

controller.js

myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {
   $http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
        $scope.jsonData = response.data;
   });


var categorieData = [];

var json = JSON.parse(jsonData); 

for (var i = 0; i <= jsonData.chargeBoxes.length - 1; i++)
{
    categorieData[i] = jsonData.chargeBoxes[i].name;
}
var myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {

$scope.barChartConfig = { categories: [] };
$http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
    $scope.jsonData = response.data.data;

    var json = JSON.parse($scope.jsonData); 
    var categorieData = [];
    for (var i = 0; i <= json.chargeBoxes.length - 1; i++)
    {
        categorieData.push($scope.jsonData.chargeBoxes[i].name);
    }
    $scope.barChartConfig.categories = categorieData;
   });
但是,当我尝试使用json数据的真实形式(使用{}的多行)时,它就不起作用了

var jsonData = {
   "status":"success",
   "data":{
      "user":{
         "username":"user1",
         "fullName":"name "       
      },
      "vehicles":[ ],
      "chargeBoxes":
      [
         {
            "id":1,            
            "name":"Station 1",
            "availability":"offline", 
         },
         {
            "id":2,            
            "name":"Station 2",
            "availability":"online", 
         },
         {
            "id":3,            
            "name":"Station 3",
            "availability":"online", 
         }
     ]
};
我真的不知道该怎么办了。首先,我想用上面这样的局部变量(var jsonData={..};因为使用一行字符串“”可以工作),然后我想直接从服务器使用这个json数据($scope.jsonData…)

谢谢!

var url=“http://”
var url = "http://<domainname>/<url>"

var jsonData = $.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success
});
var jsonData=$.ajax({ 数据类型:“json”, url:url, 数据:数据, 成功:成功 });

检查jsonData变量中的json响应。

$http.get称为异步

myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {
    $http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
        $scope.jsonData = response.data;
        // >> Put your parse code here
        var categorieData = [];
        var json = JSON.parse(jsonData); 
        for (var i = 0; i <= jsonData.chargeBoxes.length - 1; i++)
        {
            categorieData[i] = jsonData.chargeBoxes[i].name;
        }
    });
myApp=angular.module('ServiceDashboard'); myApp.controller('DataController'函数($scope、$http、$interval){ $http.get(“https://server-url.com/../login?username=..&password=..)成功(功能(响应){ $scope.jsonData=response.data; //>>把你的解析代码放在这里 分类数据变量=[]; var json=json.parse(jsonData);
对于(var i=0;i您的代码中有多个问题。首先,服务器响应数据不在

$scope.jsonData = response.data;
但你会发现它在

$scope.jsonData = response.data.data;
$http.get返回从服务器发送的响应的json对象。但是,响应主体位于要返回的响应对象的数据属性中。由于服务器的响应也具有数据属性,因此需要更深一层

其次,
var json=json.parse(jsonData);
将不起作用。在执行这行代码时,数据不可用。将其放入
$http.get()
方法的成功回调中,您就没事了

它应该是这样的:

myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {
$http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
    $scope.jsonData = response.data.data;
    for (var i in $scope.jsonData.chargeBoxes)
        categorieData.push(jsonData.chargeBoxes[i].name);
});

您需要在
$http.get()
调用的success方法中处理jsonData,因为它是异步的

controller.js

myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {
   $http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
        $scope.jsonData = response.data;
   });


var categorieData = [];

var json = JSON.parse(jsonData); 

for (var i = 0; i <= jsonData.chargeBoxes.length - 1; i++)
{
    categorieData[i] = jsonData.chargeBoxes[i].name;
}
var myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {

$scope.barChartConfig = { categories: [] };
$http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
    $scope.jsonData = response.data.data;

    var json = JSON.parse($scope.jsonData); 
    var categorieData = [];
    for (var i = 0; i <= json.chargeBoxes.length - 1; i++)
    {
        categorieData.push($scope.jsonData.chargeBoxes[i].name);
    }
    $scope.barChartConfig.categories = categorieData;
   });
var myApp=angular.module('ServiceDashboard');
myApp.controller('DataController'函数($scope、$http、$interval){
$scope.barChartConfig={categories:[]};
$http.get(“https://server-url.com/../login?username=..&password=..)成功(功能(响应){
$scope.jsonData=response.data.data;
var json=json.parse($scope.jsonData);
分类数据变量=[];

对于(var i=0;i)为什么要
var json=json.parse(jsonData);
然后使用
jsonData
而不是
json
?尝试
对于(var i=0;i)您的问题没有多大意义。您的controller.js代码无法工作,因为Ajax是异步的。“json数据的真实形式”根本不是JSON。可能的重复只是一个拼写错误。好吧,但这一定是一种在highchart控制器中可视化我从服务器(使用这种形式的JSON数据)获得的一些数据的方法!谢谢!我尝试了这个方法,但我无法在highchart ng中显示这些数据。我尝试在$scope.barChartConfig=.中访问CategreeData。。{catergorities:categorieData},但它不起作用。categorieData现在是一个全局数组吗?谢谢!但是我如何在我的控制器中使用categorieData数组?(用于可视化highchart),如下面markus所说,使用response.data.data(因为json中有一个名为“data”的属性)。我将更新我的答案。我还将for循环更改为使用
json.chargeBoxes.length
-我以前没有尝试过它。在controller->$scope.barChartConfig={..categories:我尝试了categoryData和$scope.categoryData,但它不起作用。您在
中这样做的成功()
-callback?在for循环完成后?否。在.success()-callback中,我只是解析数据值并将其推送到$scope.categileData,正如您已经解释的那样。但是对于highchart ng,我使用另一个controller.routerApp.controller('myctrl',function($scope,$timeout,$compile,$rootScope){这里是我的barChartConfig{categories:categorieData}。这就是我无法访问此变量的原因,但我不知道如何访问。。