Phonegap onclick无效

时间:2015-11-04 03:36:15

标签: php android cordova

我是关于phonegap的新手,我的按钮问题很严重。当我在浏览器上执行它完美并正确插入我的数据库,但是当我使用phonegap开发人员它不起作用,没有任何反应...有人可以帮我吗?

HTML CODE:

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

?>

1 个答案:

答案 0 :(得分:3)

这里可能存在一些问题......您的Ajax请求将转到localhost,设备本身就是设备本身。您需要将其替换为运行PHP的计算机的IP地址或主机名,并确保手机可以看到该计算机(通常是将它们放在同一个无线网络上)。

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

kCLAuthorizationStatusAuthorizedWhenInUse

要在iOS 9上运行,您还需要为iOS 9引入的App Transport Security(ATS)配置适当的例外,前提是您的服务器不受SSL保护。要执行此操作,请使用要允许非SSL连接的服务器列表修改项目的plist。所以你可以在iOS项目的.plist中添加这样的东西:

<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">

对这两个问题的正确讨论是here