2015-11-04 88 views
0

我是phonegap上的新手,我遇到一个按钮出现问题。当我在浏览器上执行时,它工作完美,并正确插入到我的数据库中,但是当我使用phonegap开发者时,它不起作用,没有任何反应......有人可以帮助我吗?Phonegap onclick不能正常工作

HTML代码:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="format-detection" content="telephone=no" /> 
    <meta name="msapplication-tap-highlight" content="no" /> 
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
    <!--<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/form_cadastrar.css" /> 
    <link rel="stylesheet" href="css/jquery.mobile-1.2.1.min.css" /> 
    <script src="js/jquery-1.8.3.min.js"></script> 
    <script src="js/jquery.mobile-1.2.1.min.js"></script> 

    <title>Cadastro</title> 
</head> 
<body> 
    <form id="formCliente" > 
     <div id="control"> 
      <div class="control-group"> 
       <div class="cad-titulo"> 
        <span>Posso te conhecer?</span> 
       </div> 
      </div>    
      <div class="control-group"> 
       <label class="control-label"> 
        Nome 
       </label> 
       <div class="controls"> 
        <input type="text" autofocus name="nome" id="nome" class="campos" required="required"> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        Sobrenome 
       </label> 
       <div class="controls"> 
        <input type="text" name="sobrenome" id="sobrenome" class="campos" required="required"> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        CPF 
       </label> 
       <div class="controls"> 
        <input type="text" placeholder="888.888.888-88" name="cpf" id="cpf" class="campos" required="required"> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        Email 
       </label> 
       <div class="controls"> 
        <input type="email" name="email" id="email" class="campos" required="required"> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        Crie&nbsp;uma&nbsp;senha 
       </label> 
       <div class="controls"> 
        <input type="password" name="senha" id="senha1" class="campos"> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        Repita&nbsp;a&nbsp;senha 
       </label> 
       <div class="controls"> 
        <input type="password" name="senha2" id="senha2" class="campos"> 
       </div> 
      </div> 
      <div class="control-group"> 
       <input onclick="enviar()" type="button"><span>ENVIAR</span></button> 
       </div> 

      <div class="control-group"> 
       <button onClick="history.go(-1)" type="button" class="btn btn-danger"><span>VOLTAR</span></button> 
       </div> 
      </div> 

     </div> 

     </form> 

    </body> 

</html>  

     <script> 
function enviar(){ 
var formula = $('#formCliente').serialize(); 

$.ajax({ 

    type:'POST', 
    data: formula, 
    url:'http://localhost/teste/www/cadastrar.php', 
    success: function(data){ 

    if(data == '' || data == 0){ 
     alert('Occoreu um erro no Banco de Dados'); 
     window.location = ""; 
    } 

    if(data == 1){ 
     alert('registro salvo com sucesso'); 
     window.location.href = "login.html"; 
     } 


    } 


}); 

} 

</script> 

PHP代码:

<?php 
$hostname = 'localhost'; 
$username = 'root'; 
$password = 'pass'; 
$database = 'mydb'; 

try { 
$pdo = new PDO("mysql:host=$hostname;dbname=$database", $username,  
$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 
    //echo 'Conexao efetuada com sucesso!'; 
} 
catch(PDOException $e) 
{ 
    echo $e->getMessage(); 
} 

$sql = 'INSERT INTO usuario (nome, sobrenome, cpf, email, senha)' ; 
$sql .= 'VALUES (:nome, :sobrenome, :cpf, :email, :senha)'; 

try { 

$recebeConexao = $pdo->prepare($sql); 

$recebeConexao->bindParam(':nome', $_POST['nome'], PDO::PARAM_STR); 
$recebeConexao->bindParam(':sobrenome', $_POST['sobrenome'], 
PDO::PARAM_STR); 
$recebeConexao->bindParam(':cpf', $_POST['cpf'], PDO::PARAM_STR); 
$recebeConexao->bindParam(':email', $_POST['email'], PDO::PARAM_STR); 
$recebeConexao->bindParam(':senha', $_POST['senha'], PDO::PARAM_STR); 

$recebeConexao->execute(); 

if($recebeConexao == true){ 
$cadastro = 1; 
}else{ 
$cadastro = 0; 
} 


} catch (PDOException $ex) { 
echo "Erro inserção"; 
} 
echo (json_encode($cadastro)); 

?> 

回答

3

你有潜在的一对夫妇在这里的问题......你的Ajax请求会为localhost该设备将成为设备本身。您将需要用您的PHP运行的计算机的IP地址或主机名替换它,并确保手机可以看到该计算机(通常使它们都位于相同的无线网络上)。

此外,如果您在Android和/或iOS上使用Cordova 5,则可能还需要添加适当的内容安全策略元标记以允许向您的服务器发送Ajax请求。例如,添加这样的事情到您的index.html的头部为您科尔多瓦应用:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://YOUR_SERVER_HOSTNAME_OR_IP_ADDRESS"> 

在iOS 9运行,你也将要配置适当的例外应用程序传输安全(ATS),其如果您的服务器没有使用SSL进行保护,则iOS 9会引入此问题要做到这一点,您可以使用您希望允许非SSL连接的服务器列表修改项目的plist。所以,你会加入类似这样的IOS项目的.plist:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSExceptionDomains</key> 
    <dict> 
     <key>yourservergoeshere.com</key> 
     <dict> 
     <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
     <true/> 
    </dict> 
    </dict> 
</dict> 

这两个问题的适当讨论here

+0

它起作用了,我不知道该如何谢谢你,自上周以来我试图解决这个问题(试用了几乎所有的东西),而且因为这个原因,我几乎在大学上失去了我的学期。非常感谢你! –