Phonegap android ajax不适用于Android设备

时间:2014-06-05 11:32:13

标签: php android jquery ajax cordova

我创建了一个示例,其中我做了一个简单的ajax调用来向Android应用程序发送数据。 这是ajax函数:

function testServer()
{
    var db = window.openDatabase("Database", "1.1","Test Exmple", 500000);
    $.ajax({
        url: urlServer+'getTest.php',
        contentType : 'application/json',
        dataType: 'json',
        data : {companyCode : 'Template'},
        success : function(data){
            alert(data);
        },
        error : function(xhr, ajaxOptions, thrownError) {

            console.log("Server is not responding... Please try after sometime"+thrownError);

        }
    });
}

这是php代码:

<?
 require_once ('connect.php');
 include("AES.class.php");
 $companyurl =$_REQUEST['companyCode'];

 $data = $companyurl;

 require_once('JSON.php');
 $json = new Services_JSON();
  echo ($json->encode($data));
 ?>

这是logcat:

 06-05 16:59:27.165: E/CordovaWebView(23703): CordovaWebView: TIMEOUT ERROR!
 06-05 16:59:27.165: D/Cordova(23703): CordovaWebViewClient.onReceivedError: Error code=-6 Description=The connection to the server was unsuccessful. URL=file:///android_asset/www/TestComp.html
 06-05 16:59:27.165: D/DroidGap(23703): onMessage(onReceivedError,{"errorCode":-6,"url":"file:\/\/\/android_asset\/www\/TestComp.html","description":"The connection to the server was unsuccessful."})
 06-05 16:59:27.255: D/skia(23703): notifyPluginsOnFrameLoad not postponed
 06-05 16:59:27.416: D/SoftKeyboardDetect(23703): Ignore this event
 06-05 16:59:27.416: D/CordovaLog(23703): Server is not responding... Please try after sometime
 06-05 16:59:27.416: I/Web Console(23703): Server is not responding... Please try after sometime at file:///android_asset/www/TestComp.html:24
 06-05 16:59:27.426: D/Cordova(23703): onPageFinished(file:///android_asset/www/TestComp.html)
 06-05 16:59:27.426: D/DroidGap(23703): onMessage(onPageFinished,file:///android_asset/www/TestComp.html)
 06-05 16:59:27.486: I/Adreno200-EGLSUB(23703): <ConfigWindowMatch:2078>: Format RGBA_8888.
 06-05 16:59:27.496: D/memalloc(23703): ashmem: Mapped buffer base:0x51f47000 size:675840 fd:124
 06-05 16:59:27.526: D/OpenGLRenderer(23703): has fontRender patch
 06-05 16:59:27.596: D/OpenGLRenderer(23703): has fontRender patch
 06-05 16:59:27.626: D/memalloc(23703): ashmem: Mapped buffer base:0x5460c000 size:675840 fd:130
 06-05 16:59:28.687: D/memalloc(23703): ashmem: Mapped buffer base:0x5470c000 size:675840 fd:133

但它在模拟器上完美运行。

任何人都可以帮我找到问题。

提前致谢

3 个答案:

答案 0 :(得分:0)

参考:http://rickluna.com/wp/2012/04/solving-the-connection-to-the-server-was-unsuccessful-error-in-androidphonegap/

super.setIntegerProperty("loadUrlTimeoutValue", 60000);
super.loadUrl("file:///android_asset/www/index.html");

答案 1 :(得分:0)

看起来像跨域请求。 因此,尝试从&#39; json&#39;更改数据类型到&#39; jsonp &#39;。 在这里,我更新了您的代码:

function testServer()
{
var db = window.openDatabase("Database", "1.1","Test Exmple", 500000);
$.ajax({
    url: urlServer+'getTest.php',
    contentType : 'application/json',
    dataType: 'jsonp',
    data : {companyCode : 'Template'},
    success : function(data){
        alert(data);
    },
    error : function(xhr, ajaxOptions, thrownError) {

        console.log("Server is not responding... Please try after     sometime"+thrownError);

    }
});
}

答案 2 :(得分:0)

伙计感谢您的帮助,代码中没有错误问题,防火墙刚刚访问防火墙端口并且工作正常。 再次感谢您的帮助。