AngularJs发布请求获取错误

时间:2016-05-18 15:33:29

标签: javascript php angularjs

我正在尝试通过角度js向登录api发出POST请求。

<form method="post" ng-submit="doLogin()">
      <div class="list">
        <label class="item item-input">
          <span class="input-label">Username</span>
          <input type="text" ng-model="loginData.user_mail">
        </label>
        <label class="item item-input">
          <span class="input-label">Password</span>
          <input type="password" ng-model="loginData.password">
        </label>
        <label class="item">
          <button class="button button-block button-positive" type="submit" >{{loginTxt}}</button>
        </label>
      </div>
      </form>

Controller.js

$scope.doLogin=function(){
     $http({
          method  : 'POST',
          url     : 'http://examplemns.com/customer/api/v1/login/login',
          data    : $scope.loginData, //forms user object
          timeout:20000
         })
         .success(function(response){

              alert(JSON.stringify(response));
           })
           .error(function(err){
                console.log(JSON.stringify(err));
                alert("Network error");

             });
         }

但即使用户名和密码正确,我也会得到invalid username响应。 我通过postman插件检查api它的工作正常,但是当有角度时我会失效。

以下是示例输入

user_mail:avm@gmail.com
password:123456

当使用postman插件尝试此输入时,我将得到正确的响应

{
  "status": 1,
  "message": "Done",
  "data": {
    "name": "A.V.M TOURIST HOME",
    "username": "avm@gmail.com",
    "id": "37",
    "key": "cos4ok88woo0kcw40cog0s4gg4skogscso8848ok"
  }
}

但是当尝试使用相同输入的angularjs post i时,我将得到此响应

{"status":0,"message":"Invalid username"}

请帮帮我:(

更新

我需要将我的数据转换为application / x-www-form-urlencoded而不是json(来自评论),因为我使用这种方式。

var data= {user_mail:$scope.loginData.user_mail,password:$scope.loginData.password};
$http({
              method  : 'POST',
              url     : 'http://examplemns.com/customer/api/v1/login/login',
              data    : data, 
              timeout:20000
             })
             .success(function(response){

                  alert(JSON.stringify(response));
               })
               .error(function(err){
                    console.log(JSON.stringify(err));
                    alert("Network error");

                 });

但我会再次得到相同的

邮递员的请求和回复的屏幕截图

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个

    $scope.loginData = {};
    $scope.doLogin=function(){

         $http({
              method  : 'POST',
              url     : 'http://examplemns.com/customer/api/v1/login/login',
              data    : $scope.loginData, //forms user object ,
              headers: {'Content-Type': 'application/x-www-form-urlencoded'}

             })
             .success(function(response){
                  console.log(response);
             })
             .error(function(err){
                    console.log(err);
              });
            }

$scope.loginData = {};
var data = $scope.loginData;
$http.post('/examplemns.com/customer/api/v1/login/login', data).then(successCallback, errorCallback);

也可以尝试

  <input type="text" ng-model="user_mail">
  <input type="password" ng-model="password">

  var data = {};
  data.user_mail = $scope.user_mail;
  data.password = $scope.password;

  $http.post('/examplemns.com/customer/api/v1/login/login',   data).then(successCallback, errorCallback);

答案 1 :(得分:0)

尝试更改以下行:

data    : $scope.loginData, //forms user object

为:

data    : JSON.stringify($scope.loginData), //forms user object