2016-05-18 55 views
2

我試圖做一個POST請求通過角JS登錄API。AngularJs 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響應。 我通過郵遞員插件其做工精細檢查API,但與角來了,我會得到無效。

這裏是樣本輸入

user_mail:[email protected] 
password:123456 

當嘗試郵遞員插件此輸入我會得到正確的響應

{ 
    "status": 1, 
    "message": "Done", 
    "data": { 
    "name": "A.V.M TOURIST HOME", 
    "username": "[email protected]", 
    "id": "37", 
    "key": "cos4ok88woo0kcw40cog0s4gg4skogscso8848ok" 
    } 
} 

而是通過angularjs當試圖職位,我以相同的輸入我會得到這個迴應

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

請幫我:(

UPDATE

我需要我的數據轉換爲應用程序/ x-WWW的形式了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

+0

你能告訴我們無論是屏幕截圖或文本您的郵遞員請求的版本? – sam

+0

是否郵遞員發送數據像角意志JSON? – Paqman

+0

您可以通過點擊「生成」從郵遞員的響應已經包括在我的問題 – sam

回答

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); 
       }); 
      } 

OR

$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); 
+0

他需要設置爲表單數據,而不是的簡單的JSON對象 – sam

+0

它沒有工作:( –

+0

'{ 「數據」:{ 「狀態」:0, 「消息」: 「無效的用戶名」}, 「狀態」:200, 「配置」:{「方法「:」 POST」, 「transformRequest」:[空], 「transformResponse」:[空], 「URL」: 「HTTP:// smaplessss,COM /客戶/ API/V1 /登錄/登錄」, 「數據」: { 「user_mail」: 「[email protected]」, 「密碼」: 「123456」}, 「頭」:{ 「接受」: 「應用/ JSON,純文本/,*/*」, 「內容類型」 : 「應用/ JSON的;字符集= UTF-8」}, 「狀態文本」: 「OK」}' –

0

嘗試改變下面一行:

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

到:

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