Angularjs调用restful api(post)但没有成功的错误请求

时间:2016-02-23 05:11:32

标签: angularjs json rest basic-authentication restful-authentication

我必须使用基本身份验证调用一个安静的API“我认为auth正在运行” 需要这个特殊的自定义标题:“X-AppGlu-Environment:staging”我不知道如何放置标题“然后我需要发布到一个URL,这种格式的数据:

Body:

{
  "params": {
   "stopName": "what you want search"
 }
}

让我们看看我的代码“它还没有结构化”

控制器:

'use strict';

 angular.module('myApp', ['base64'])
 .controller('transportController', function($scope, $http, $base64){
$scope.$watch('search', function() {
  fetch();
});



function fetch(){

var data = {
  "params": {
   "stopName": $scope.search
 }
}

  $http.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
  var encoded = $base64.encode("xxxx:xxxx");
  $http.defaults.headers.common.Authorization = 'Basic ' + encoded;
  $http.post("the url", data)
 .then(function(response){ $scope.details = response.data; });

  }

  });

观点:

    <div class="input-group search-bar">
      <input type="text" ng-model="search" ng-model-options="{ debounce: 800 }" onclick="select()" class="form-control" placeholder="Enter destiny" autofocus />
      <span class="input-group-addon bar-style"><i class="glyphicon glyphicon-search"></i></span>

当我尝试搜索某些内容时收到错误400错误请求,我认为是我正在尝试发送的对象格式,但如果是,我不知道要更改什么。

1 个答案:

答案 0 :(得分:0)

现在正在工作我改为这个结构

function fetch(){

var data = { 
      "params":{
        "stopName": "%"+$scope.search+"%"
    }
}

var encoded = $base64.encode("xxxx:xxxx");
$http({
  url: "url",
  headers : {
    "X-AppGlu-Environment":"staging",
    "Authorization": "Basic "+encoded,
    "Content-Type" : "application/json; charset=utf-8"
  },
  method: 'POST',
  data: { 
    "params":{
      "stopName": "%"+$scope.search+"%"
    }
  }
}).then(function(response){ 
    $scope.details = response.data.rows; 
});


 // $http.get("http://www.omdbapi.com/?s=" + $scope.search)
  //.then(function(response){ $scope.related = response.data; });
}