2015-07-11 45 views
0

我試圖製作一個Phonegap應用程序,它具有一個簡單的登錄窗體,它應該理想地連接到一個PHP窗體並檢索用戶角色。PHP MYSQL JSON Phonegap登錄應用程序

我想澄清以下幾點:

  1. 我的登錄頁面,用PHP編寫的正常工作。
  2. 當給予適當的憑證獲取參數,它返回正確的JSON標籤:

    {"update":true,"msg":"","role":"admin"} 
    

這裏是我的PhoneGap應用程序的HTML代碼。請幫助我在哪裏出錯。假設用戶名是用戶名,密碼是密碼。

<!DOCTYPE html> 
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
     <link rel="stylesheet" type="text/css" href="css/index.css" /> 
     <title>Hello World</title> 
    </head> 
    <body> 
     <div class="app"> 
      <form> 
     <input id="username" name="username"placeholder="Email"></input> 
     <input id="password" name="password"type="password" placeholder="Password"></input> 
     <input type="button" value="Login" id="btnLogin"/> 
    </form> 
<div id="myDate"></div> 
     </div> 
     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
     <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script> 
     <script> 
$(document).ready(function() 
{ 
$("#btnLogin").click(function() 
{ 
    $("#myDate").text("Reached Here"); 
    $.ajax({ 
url:"https://myurl.com/api-file.php", 
type:"GET", 
dataType:"json", 
data:{username:"username",password:"password"}, 
ContentType:"application/json", 
success:function(response) 
{ 
    alert(JSON.stringify(response)); 
}, 
error: function(error) 
{ 
    alert(JSON.stringify(error)); 
} 
}) 
}); 
}); 
    </script> 

    </body> 
</html> 

如何在App應執行的,當我按一下按鈕,它會調用jQuery代碼,生成一個Ajax請求的PHP文件,並檢索一個JSON字符串,我打算只彈出的警告,截至目前。

但是,單擊按鈕確實沒有任何作用。

我現在硬編碼的用戶名和密碼,但可以並將在稍後的領域採取它。不過,在我這樣做之前需要先測試一下。請幫助我哪裏錯了。

+0

http://www.stackoverflow.com/questions/25528109/login-with-phonegap-ajax-php-and-mysql-not-working?rq=1 – kritya

回答

0

如果什麼也沒發生,那麼問題是點擊功能,否則AJAX調用會顯示錯誤。我說試着擺脫$(document).ready(function()並將js/jquery-2.1.4.min.js放在頭部(順便說一句,沒有起始標籤)。

+0

這工作完美。謝謝。 –

0

注意:如果您包含您收到的錯誤或您的代碼未按預期運行的方式說明,這將有很大幫助。

這就是說,我可以立即在代碼中看到的唯一錯誤是您的url參數格式不正確。取而代之的是:

url:https:"//myurl.com/api-file.php", 

它應該是這樣的:

url:"https://myurl.com/api-file.php", 
+0

我同意你的聲明,所以重新編輯這個問題。確實是錯誤的,但顯然,糾正這似乎並沒有幫助。也更新了新的代碼。更多的建議? –