简体   繁体   English

phonegap连接到服务器上的php

[英]phonegap connect to a php on a server

Hello I am doing an App that get some information and trys to connect to php located on a server. 您好,我正在做一个获取一些信息并尝试连接到服务器上php的应用程序。

here is the code: 这是代码:

<!DOCTYPE HTML>
<html>
 <head>
  <title>PhoneGap</title>
  <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>
  <script src="js/jquery-1.js"></script>
    <script>
    $(document).ready(function () {
        $("#btn").click( function() {
        alert('hello hello');

        $.ajax({
            url: "192.168.0.106/test/testget.php",        
            data: {
                t:"lalalala"
            },
            datatype: "json",
            success: function (status)
            {
                if (status.success == false)
                {
                    alert("Failure!");
                }
                else 
                {
                    alert("Success!");
                }
            }
        });

    });
    });
</script>


 </head>
 <body style="background: url(BG.jpg) no-repeat;background-size: 100%;" onload="onBodyLoad()">
<button style="background-color:grey;margin-left:75%;display:block;margin-top:5%;margin-bottom:0%" type="button" onclick="alert('Formulario Enviado')">Enviar</button> 

 <u><h3>TAREA (MOLINO 3)</h3></u> 
  <div style="height:378px;width:305px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;">
<form> 
<table border="1">
 <tr>
 <th>Verificación de banda transportadora</th>
 <th>OK</th>
 <th>NO</th>
 </tr>

<tr>
<td>Fugas del material y estado de los rodillos.</td>
<td><input type="radio"  name="DB1"></td>
<td><input type="radio"  name="DB1"></td>
</tr>
<tr>
<td>Desalineamiento de la cinta o banda.</td>
<td><input type="radio"  name="DB2"></td>
<td><input type="radio"  name="DB2"></td>
</tr>
<tr>
<td>La superficie de la banda.</td>
<td><input type="radio"  name="DB3"></td>
<td><input type="radio"  name="DB3"></td>
</tr>
<tr>
<td>El ajuste de la guarda.</td>
<td><input type="radio"  name="DB4"></td>
<td><input type="radio"  name="DB4"></td>
</tr>
<tr>
<td>El ajuste del rascador.</td>
<td><input type="radio" name="DB5" ></td>
<td><input type="radio" name="DB5" ></td>
</tr>
<tr>
<td>La temperatura del motor.</td>
<td><input type="radio"  name="DB6"></td>
<td><input type="radio"  name="DB6"></td>
</tr>
<tr>
<td>La vibración de la banda.</td>
<td><input type="radio"  name="DB7"></td>
<td><input type="radio"  name="DB7"></td>
</tr>
<tr>
<td>El sensor de movimiento.</td>
<td><input type="radio"  name="DB8"></td>
<td><input type="radio"  name="DB8"></td>
</tr>
<tr>
<td>El funcionamiento de las paradas de emergencia.</td>
<td><input type="radio" name="DB9" ></td>
<td><input type="radio"  name="DB9"></td>
</tr>
<tr>
<td>El estado del tambor de cola.</td>
<td><input type="radio"  name="DB10"></td>
<td><input type="radio"  name="DB10"></td>
</tr>
<tr>
<td>El revestimiento del tambor motriz.</td>
<td><input type="radio" name="DB11" ></td>
<td><input type="radio" name="DB11" ></td>
</tr>
<tr>
<td>Los ruidos anormales en el equipo.</td>
<td><input type="radio" name="DB12" ></td>
<td><input type="radio" name="DB12" ></td>
</tr>
<tr>
<td>La lubricación del tambor de la chumacera.</td>
<td><input type="radio"  name="DB13"></td>
<td><input type="radio"  name="DB13"></td>
</tr>
<tr>
<td>Las fugas del lubricante.</td>
<td><input type="radio" name="DB14" ></td>
<td><input type="radio" name="DB14" ></td>
</tr>
<tr>
<td>El estado de las luminarias en la zona (luminarias rotas y funcionales).</td>
<td><input type="radio" name="DB15" ></td>
<td><input type="radio" name="DB15" ></td>
</tr>
<tr>
<td>El material centrado en la cinta.</td>
<td><input type="radio" name="DB16" ></td>
<td><input type="radio" name="DB16" ></td>
</tr>
<tr>
<td>El material que está por debajo de la banda (apartarlo).</td>
<td><input type="radio" name="DB17" ></td>
<td><input type="radio" name="DB17" ></td>
</tr>
<th align="center">Comentarios</th>
 </tr>
    <td><textarea rows="4" cols="20">
</textarea>
    </td>
</div>
<input id="btn" type="submit" value="pressme">
</form>
</table>

<br>


 </body>
</html>

The php file located at: 192.168.0.106/test/testget.php contains: 位于192.168.0.106/test/testget.php的php文件包含:

<?php echo $_GET['t'] ?>

The thing with all this is not the part of the connection is that I don't get the message over the script 所有这些都不是连接的一部分,因为我没有通过脚本获得消息

UPDATE: The problem was located at the phone gap not getting the right plugins to work. 更新:问题出在电话缺口上,无法使正确的插件正常工作。

UPDATE 2: MAKE SURE you are using the right jquery files 更新2:确保您使用的是正确的jquery文件

You need to check this things: 您需要检查以下内容:

1. Add error callback (or fail if you are using jQuery version 1.8 +) to your ajax call, just like success: 1.将错误回调(如果您使用的是jQuery 1.8 +,则失败)添加到您的ajax调用中,就像成功一样:

    error: function (request,error) {
        alert('Network error has occurred please try again - error:' + error);
    }

2. Handle different status codes with statusCode postback: 2.使用statusCode回传处理不同的状态码:

    statusCode: {
        400: function() {
            alert("Bad request!");
        },
        401: function() {
            alert("Unauthorized!");
        },
        403: function() {
            alert("Forbidden!");
        },
        404: function() {
            alert("Page not found!");
        },                
        408: function() {
            alert("Request Timeout!");
        },
        200: function() {
            alert("page reached");
        },    
    }

If there's an error you will probably get some of this codes. 如果有错误,您可能会得到一些这样的代码。

3. If you haven't add this line into your AndroidManifest.xml: 3.如果尚未将此行添加到AndroidManifest.xml中:

<uses-permission android:name="android.permission.INTERNET" />

It will allow you to access an internet and your local network 它将允许您访问互联网和本地网络

4. If you are testing this on a real mobile phone 4.如果要在真实的手机上进行测试

Don't forget to turn your WIFI or 3G on. 不要忘记打开WIFI或3G网络。 You would be amazed how many people forget to do this. 您会惊讶于有多少人忘记这样做。

5. Check that your server accepts outside requests 5.检查您的服务器是否接受外部请求

This may sound stupid but some Windows *AMP solutions need to be put online before you can access them from the network. 这听起来可能很愚蠢,但是某些Windows * AMP解决方案需要先联网,然后才能从网络访问它们。 One of them is for example WAMP. 其中之一是例如WAMP。

try to use 尝试使用

type: 'GET'

Example : 范例:

$.ajax({
            type: 'GET',
            data: postData+'&amp;lid='+landmarkID,
            url: 'http://your-domain.com/comments/save.php',
            success: function(data){
                console.log(data);
                alert('Your comment was successfully added');
            },
            error: function(){
                console.log(data);
                alert('There was an error adding your comment');
            }
        });

If you want it to be GET variable - use 如果您希望它是GET变量-使用

datatype: "text"

instead of 代替

datatype: "json"

Otherwise to get the message you must parse JSON in your PHP script 否则,要获取消息,您必须在PHP脚本中解析JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM