Angular JS using $http() get a response, with status 200, but data is null

前端 未结 2 993
春和景丽
春和景丽 2021-01-20 16:46

I\'m using this angular code:

$http({
  method: \'get\',
  url: \'json/test.json\',
  responseType: \"json\"
}).success(function(data, status, headers, confi         


        
相关标签:
2条回答
  • 2021-01-20 17:21

    This might happen because json is not formatted properly. Try:

    [
        {
            "id": 1,
            "name": "aaaa",
            "age": 20
        },
        {
            "id": 2,
            "name": "bbbbb",
            "age": 21
        }
    ]
    

    and use

    $http.get("json/test.json").success(function (data) {
        console.log('success: ' + data)
    });
    

    or you may use your original code but without responseType as @DeborahK figured out:

    $http({
      method: 'get',
      url: 'json/test.json'
    }).success(function(data, status, headers, config) {
      console.log('Success: ' + data);
    })
    

    But anyway you would need to format your json properly, otherwise angular will raise parsing exception.

    0 讨论(0)
  • 2021-01-20 17:31

    I don't know which version of AngularJS you are using, but assuming v1.6, then try this:

    $http.get("json/test.json").then(function(response) {
        console.log('success: ' + response.data);
    }).catch(function(response) {
        console.log('error: ' + response.data);
    });
    
    0 讨论(0)
提交回复
热议问题