我创建了一个示例,其中我做了一个简单的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
但它在模拟器上运行得很好。
谁能帮我找出问题所在吗?
Thanks in advance
参考: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");
看起来像是跨域请求。因此,尝试将数据类型从'json'更改为'jsonp'。这里我更新了你的代码:
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);
}
});
}
伙计们,感谢你们的帮助,代码中没有错误,问题是防火墙,只是在防火墙中访问端口,它工作得很好。再次感谢您的帮助。