phonegap-使用eclipse readystate 0 responsetext status 0 statustext error

时间:2015-07-09 08:56:28

标签: php mysql ajax cordova phonegap-build

我试图使用ajax请求访问phonegap应用程序上的mysql数据库数据。它在php中运行得非常好但是当我尝试在phonegap上运行它时,它给了我一个恼人的错误 - 请找到代码:

                   <script>
                        $(document).ready(function () {
                            $("#btnclick").click(function () {              
                                    $.ajax({
                                        url: "http://192.168.1.16:8080/api.php",                                                                                    
                                        datatype: "json",
                                        ContentType:"application/json", 
                                        success: function(respose) 
                                        {                   
                                            alert("It works !!");
                                            alert(JSON.stringify(respose));
                                        },
                                        error: function(err) 
                                        {
                                            alert("It failed ");
                                            alert(JSON.stringify(err));
                                        }                           
                                    });                                             
                                });
                            });
                    </script>
                </head>
                <body>
                    <p>This is a paragraph content</p>

                <h1>Call API from Php</h1>
                <input type="button" value="login" id="btnclick" />
                </body>

这是我的php api:api.php

            <?php
            header('Access-Control-Allow-Origin: *');

            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "ajax01";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);

            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 

            else
            {

                    //if(isset($_GET['Username']))
                    {
                        //$username = $_GET['Username'];
                        //$password = $_GET['Password'];

                        $mysql_user = "root";
                        $mysql_password = "";
                        $database = "ajax01";


                        $link = mysqli_connect("localhost","$mysql_user","$mysql_password","$database") or die("Error " . mysqli_error($link));

                        $query = "SELECT * FROM logintable" or die("Error in the connection" . mysqli_error($link)); 


                        //execute the query. 

                        //$query = "update logintable set id='5' where id='1' " or die("Error in the connection" . mysqli_error($link)); 


                        $result = mysqli_query($link, $query); 

                        $totalRows = mysqli_num_rows($result);

                        while($row = mysqli_fetch_array($result)) 
                        { 
                          echo $row["id"] . $row["name"] . $row["password"] ;             
                        }   


                    }
            }
            ?>              

并且phonegap中的错误是:readystate 0 responsetext status 0 statustext error

虽然相同的代码在Web服务器上运行良好。帮助!!

2 个答案:

答案 0 :(得分:0)

这可能对您有所帮助

sudo cordova plugin add https://github.com/apache/cordova-plugin-whitelist

现在您已拥有此插件,请在config.xml

中添加此行
<allow-navigation href="http://192.168.1.16:8080/*" />

重建并重试!

答案 1 :(得分:0)

一切都很好,只需要编辑ANDroid manifest.xml:请检查一下:

打开以编辑AndroidManifest.xml文件。 将以下节点作为子节点添加到清单节点

                <supports-screens
                android:anyDensity="true"
                android:largeScreens="true"
                android:normalScreens="true"
            android:resizeable="true"
            android:smallScreens="true" />

            <uses-permission android:name="android.permission.CAMERA" />
            <uses-permission android:name="android.permission.VIBRATE" />
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
            <uses-permission android:name="android.permission.READ_PHONE_STATE" />
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.RECEIVE_SMS" />
            <uses-permission android:name="android.permission.RECORD_AUDIO" />
            <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
            <uses-permission android:name="android.permission.READ_CONTACTS" />
            <uses-permission android:name="android.permission.WRITE_CONTACTS" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.GET_ACCOUNTS" />
            <uses-permission android:name="android.permission.BROADCAST_STICKY" />
            Add a attribute to <activity> node, which is the child node to <application> node android:configChanges="orientation|keyboardHidden"
            Add the following <activity> node as a sibling to existing <activity> node
            <activity android:name="org.apache.cordova.DroidGap" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
                <intent-filter></intent-filter>
            </activity>