跨域提交不要想得太复杂了, 由于HTML5开始,网页版本的HTML5不能跨域
但是 HTML5+phone 转成安卓 的 eclipse环境里 ,肯定要面对跨域的问题,
不要想得太复杂了,配置jquerymobile 即可, 在引入jquery.mobile.***.js 之前 加入
<script>
$(document ).bind( "mobileinit", function() {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
</script>
下面是例子:
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" charset="UTF-8">
<link href="../jquerymobile1.4.0/jquery.mobile-1.4.0.css" rel="stylesheet" type="text/css">
<script>
$(document ).bind( "mobileinit", function() {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
</script>
<script src="../jquery.min.js" type="text/javascript"></script>
<script src="../jquerymobile1.4.0/jquery.mobile-1.4.0.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$(gettest).click(function(){
//http://117.40.138.151:520/zms/servlet/returnjsonString
$("#return1").text("jkdd");
$.ajax({
type: 'post',
url: "http://IP/zms/servlet/returnjsonString",
success: function(data, textStatus) { $("#return1").text("成功"+data+"\n"+textStatus); },
error:function(XMLHttpRequest, textStatus, errorThrown){
$("#return1").text("失败"+XMLHttpRequest+textStatus+errorThrown);
}
})
});
});
</script>
<title>HTML-ZMS</title>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>跨域提交</h1>
<!-- <a href="#" class="ui-btn">返回</a>-->
</div>
<div data-role="main" class="ui-content">
<div id="return1">显示返回的数据:暂无</div>
<input type="button" id="gettest" value="获取">
</div>
<div data-role="footer">
<h4>ZTE</h4>
</div>
</div>
</body>
</html>
服务器 是一个 servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
// String jsonstring="{userinfo: [{'datetime1':'2013-11-01','num2':323},{'datetime1':'2013-12-01','num2':23},{'datetime1':'2014-02-01','num2':432}]}";
String jsonstring="{userinfo: [{'datetime1':'第一学期','num2':43},{'datetime1':'第二学期','num2':73},{'datetime1':'第三学期','num2':47},{'datetime1':'第四学期','num2':132}]}";
System.out.println(jsonstring);
out.write(jsonstring);
out.flush();
out.close();
}
3 对了 eclipse 安卓开发环境 xml---config.xml 要增加白名单 红色字体 可以改为 <access origin="你的服务器IP,或者全部权限*>
<description>
zms@zte
</description>
<author href="http://cordova.io" email="[email protected]">
zms2014@zte
</author>
<access origin="*"/>
<!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
<content src="gap/index.html" />