2015-11-16 41 views
0

我試圖發送一個帶有$http -service angularjs的服務器上的現有用戶數據發送的登錄數據發送到服務器。用戶數據,所以(loginData),創建和現有在後端服務器上。隨着用戶的信息,我想記錄我在美孚的手機上。我怎樣才能比較本地與服務器上的現有數據輸入的數據?我的代碼還沒有工作。使用AngularJS /離子

的login服務:

myApp.factory('loginService', function($http){ 
    var endPoint = "https://www.****.***/****/****"; 
    var loginData = 
    { 
    "username": "user", 
    "password": "pass" 
    }; 

    return{ 
    login: function(username, password){ 
     return $http.post(endPoint, loginData); 
    } 
    } 
}); 

的LoginController中:

myApp.controller('LoginCtrl', function ($scope, $location, $window, loginService) { 

    $scope.logIn = function logIn(username, password) { 
    if (username !== undefined && password !== undefined) { 

     loginService.login(username, password).success(function(data) { 
     $window.sessionStorage.token = data.token; 
     $window.location.href = '/index.html'; 
     }).error(function(status, data) { 
     console.log(status); 
     console.log(data); 
     }); 
    } 
    } 
}) 

HTML:

<ion-content style="text-align: left" ng-controller="LoginCtrl"> 
    <label class="item item-input"> 
     <span class="input-label">Name:</span> 
     <input name="name" autofocus="true" required="true" type="text" 
      ng-model="username"/> 
    </label> 
    <label class="item item-input"> 
     <span class="input-label">Password:</span> 
     <input name="password" required="true" type="password" 
      ng-model="password"/> 
    </label> 
    <button id="button-1" class="button button-block button-positive" ng-click="logIn(username, password)"> 
     Login 
    </button> 
</ion-content> 

我得到的以下錯誤:

Error: Unexpected request: POST https://www.****.***/****/**** 
No more request expected 
    at $httpBackend (angular-mocks.js:1244) 
    at n (ionic.bundle.min.js:119) 
    at f (ionic.bundle.min.js:117) 
    at ionic.bundle.min.js:151 
    at n.$eval (ionic.bundle.min.js:165) 
    at n.$digest (ionic.bundle.min.js:163) 
    at n.$apply (ionic.bundle.min.js:166) 
    at HTMLButtonElement.<anonymous> (ionic.bundle.min.js:446) 
    at HTMLButtonElement.Gf.c (ionic.bundle.min.js:66) 
    at n (ionic.bundle.min.js:22)(anonymous function) @ ionic.bundle.min.js:139(anonymous function) @ ionic.bundle.min.js:111(anonymous function) @ ionic.bundle.min.js:151n.$eval @ ionic.bundle.min.js:165n.$digest @ ionic.bundle.min.js:163n.$apply @ ionic.bundle.min.js:166(anonymous function) @ ionic.bundle.min.js:446Gf.c @ ionic.bundle.min.js:66n @ ionic.bundle.min.js:22t @ ionic.bundle.min.js:22l @ ionic.bundle.min.js:22 

loginController.js:13 undefined 

我仍然沒有得到HTTP狀態代碼200。代碼有什麼問題?

千恩萬謝

回答

0

是U確保您的API端點支持POST請求。 ?好像你正在發送POST請求,你的API終點希望別的東西。

,你可以嘗試像

curl --data "username=value1&password=value2" https://www.****.***/****/****

從控制檯

+0

是,該API支持'POST'請求​​的工作代碼。我嘗試了'curl'命令,並且得到了錯誤:'Uncaught SyntaxError:Unexpected identifier'。我的代碼(服務和控制器)是否正常? – Ramosta

0

這被稱爲CORS isssue測試。 你可以從下面的鏈接更多的想法。 Handling CORS issues with ionic

這是因爲您必須爲此設置代理服務器。 因爲您正在一個域上運行您的應用程序,並且ajax正在調用另一個域。

這是可以解決的,只需遵循幾個步驟

  1. 打開你離子。項目文件

  2. 添加以下代碼按您的要求

    { 「名」: 「PROJECT_NAME」, 「APP_ID」: 「」, 「代理」: { 「路徑」:「/yoursite /服務/」, 「proxyUrl」: 「http://www.yoursite.com/yoursite/services/」 } ] }

這意味着它取代http://locahost:8100/yoursite/serviceshttp://www.yoursite.com/yoursite/services/

希望可以對你有所幫助

下面是將數據發送到服務器

$http({ 
     method: 'GET', 
     url: $loginUrl, 
     headers: { 
      'Content-Type': "application/json" 
     } 
    }).success(function(){ 
      //perform some action on success 
    }) 
+0

你認爲我的代碼是正確的嗎?或者你有一個改進建議? – Ramosta

+0

看看編輯...它現在可能會有幫助..它是我的工作代碼... @Ramosta –