【问题标题】:Get data from ajax request to Angular从 ajax 请求获取数据到 Angular
【发布时间】:2015-12-16 12:13:03
【问题描述】:

我有 2 个请求

$.ajax({
    type: "POST",
    url: "http://sandbox.gasvisor.com:9988/uaa/oauth/token",
    data: "grant_type=client_credentials",
    headers: { 'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization':'Basic SVRXRV9XRUJfQVBQOml0d2VfY2xpZW50' },
    success:function(data){
        console.log(data);
        getData(data.access_token);
    }
})

function getData(acess_token){
    $.ajax({
        type: "GET",
        url: "http://sandbox.gasvisor.com/api/v2/gobjects/search/withinRadius?longitude=24.711117&latitude=48.922633&radius=10",
        headers: { 
            'Authorization':'bearer'+acess_token },
            success:function(data){
                console.log(data);
            }
        })    
}

他们从服务器返回 json。我需要从 Json 文件中获取数据到我的 Angular 列表中。请帮助我。

【问题讨论】:

  • 使用Angular方式用$http做Ajax,请添加Angular相关代码。
  • 我不知道如何使用 $http。我需要从 Json 获取数据并使用 ng-repeat 将其发布到 Angular 中。我知道如何使用 ng-repeat,但找不到如何使用 $http 执行我的请求
  • 使用这个链接看看你能学到什么:docs.angularjs.org/api/ng/service/$http

标签: javascript jquery angularjs json ajax


【解决方案1】:

这是您的一个电话的简单示例:

$http({
  method: 'POST',
  url: 'http://sandbox.gasvisor.com:9988/uaa/oauth/token',
  data: 'grant_type=client_credentials',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization':'Basic SVRXRV9XRUJfQVBQOml0d2VfY2xpZW50' },
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    $scope.response = response;
  }, function errorCallback(err) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    $scope.error = err;
});

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 2015-11-13
    • 2023-03-26
    • 2017-10-24
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多