2016-11-23 80 views
0

我正在開發一個需要登錄的應用程序,所以我有一個控制器發佈在服務器上的郵件和密碼插入在窗體中,如果登錄成功我打印在$ scope.response字符串"T"和在控制器我應該驗證字符串是否爲"T",然後將用戶重定向到主頁。如何做一個登錄離子與http post?

這是我的代碼:

FORM HTML

<form ng-submit="submit()"> 
       <div class="list"> 
        <label class="item item-input"> 
        <span class="input-label">Email</span> 
        <input type="email" name="mail" ng-model="data.mail"> 
        </label><br> 
        <label class="item item-input"> 
        <span class="input-label">Password</span> 
        <input type="password" name="pwd" ng-model="data.pwd"> 
        </label><br> 
        <input type="hidden" name="funzione" value="login" ng-model="data.funzione"> 
        <input class="button button-calm" type="submit" name="submit" value="Login"> 
       </div> 
      </form> 

控制器

.controller('LoginCtrl', function($scope, $http, $state) { 
    $scope.data = {}; 
    $scope.submit = function(){ 
     var link = 'http://localhost/ShuttleFIX/api.php'; 
     $http.post(link, {mail : $scope.data.mail}, {pwd : $scope.data.pwd}, {funzione : $scope.data.funzione}) 
      .then(function (res){ 
       $scope.response = res.data; 
       if ($scope.response == "T"){ 
        state.go('main'); 
       } 
      }); 
    }; 
}); 

API

/* 
* connection to DB 
*/ 
if (isset($_SERVER['HTTP_ORIGIN'])) { 
     header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); 
     header('Access-Control-Allow-Credentials: true'); 
     header('Access-Control-Max-Age: 86400'); // cache for 1 day 
    } 
    // Access-Control headers are received during OPTIONS requests 
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { 
     if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) 
      header("Access-Control-Allow-Methods: GET, POST, OPTIONS");   
     if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) 
      header("Access-Control-Allow-Headers:  {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); 
     exit(0); 
    } 
    $postdata = file_get_contents("php://input"); 
if (isset($postdata)) { 
$request = json_decode($postdata); 
$funzione = $request->funzione; 
if ($funzione == "login") { 
    $mail = $request->mail; 
    $pwd = $request->pwd; 
    $query = "SELECT * FROM utente WHERE mail = '$mail' AND pwd = '$pwd'"; 
    $res = mysql_query($query);               
    if ($res && mysql_num_rows($res) > 0) { 
     $response = "T"; 
     echo $response; 
    } 
} 

但它不起作用。

我該如何解決這個問題?

感謝的

UPDATE

我從控制檯收到此錯誤:XMLHttpRequest cannot load http://localhost/<path>/api.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

回答

0

常見的服務器端issue.Add在服務器端這些頭。

Access-Control-Allow-Headers: Content-Type 
Access-Control-Allow-Methods: GET, POST, OPTIONS 
Access-Control-Allow-Origin: * 

注:這裏看一看,尋求更多幫助,https://www.w3.org/wiki/CORS_Enabled

而且一步一步的指導可以在這裏找到:http://www.nikola-breznjak.com/blog/codeproject/posting-data-from-ionic-app-to-php-server/

+0

感謝的,但現在看來,它的工作原理@Antonis – Edoardo

+0

我編輯我的答案與鏈接。希望這個幫助。此問題也出現在瀏覽器中。您可以使用Chrome擴展程序https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi來解決此問題。 – Antonis