在Android上执行Ajax请求时不返回传输错误 [英] Making Ajax request on Android returns No Transport error

查看:383
本文介绍了在Android上执行Ajax请求时不返回传输错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上使用Phonegap开发了一个示例应用程序。

当点击按钮时,会向服务器发出Ajax请求( http://192.168.0.199: 8080 / test.php )。

test.php 只是回应 hello world

我使用 Jquery 1.5 进行Ajax调用。

I have developed a sample app on Android using Phonegap.
When clicked on a button, an Ajax request is made to the server (http://192.168.0.199:8080/test.php).
The test.php just echoes hello world.
I am using Jquery 1.5 for making the Ajax call.

以下是Ajax调用的代码:

Here is the code for the Ajax call:

$.ajax({  
 url:"http://192.168.0.199:8080/test.php",  
 beforeSend: function(x) {
 alert("The URL "+url);  
},
type:'POST',  
crossDomain: true,
success:function(data) {  
  alert(data);  
},  
error:function(XMLHttpRequest,textStatus, errorThrown) {     
 alert("Error status :"+textStatus);  
 alert("Error type :"+errorThrown);  
 alert("Error message :"+XMLHttpRequest.responseXML);  
}
});    

但是,每次调用此函数时,都会抛出一个错误:

But, every time this function is called, it throws an error:

错误类型:无运输

错误消息:undefined

错误状态:错误

注意:我已经添加了INTERNET权限在清单文件中。我正在使用运行Android 1.6的G1设备

提前感谢

推荐答案

从jQuery 1.5开始,使用了一个新的AJAX / XHR实现。和Android 1.5 / 1.6不说它支持CORS(跨域调用)。所以一个修复的方式是通知jQuery它支持cors:

As of jQuery 1.5 a new AJAX / XHR implementation is in use. And Android 1.5 / 1.6 doesn't say it supports CORS (crossdomain calling). So a way to 'fix' that is to inform jQuery it does support cors:

jQuery.support.cors = true;

我无法重现2.1及以上的问题。所以我期望从该版本的XHR实现确实说它支持cors。

I couldn't reproduce the problem on 2.1 and up. So I expect the XHR implementation from that version on does say it does support cors.

这篇关于在Android上执行Ajax请求时不返回传输错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆