html5+jquerymobile+phonegap 安卓 ajax /js 跨域

这篇博客介绍了如何在HTML5+jQueryMobile+PhoneGap的安卓应用中解决AJAX跨域问题。通过配置jQueryMobile,设置`$.support.cors = true;`和`$.mobile.allowCrossDomainPages = true;`,并在Eclipse的安卓开发环境中配置XML的白名单,可以实现跨域请求。示例代码展示了如何进行AJAX请求以及服务器端Servlet的响应处理。
摘要由CSDN通过智能技术生成

跨域提交不要想得太复杂了, 由于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="dev@cordova.apache.org">
      zms2014@zte
    </author>

    <access origin="*"/>
 
    <!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
    <content src="gap/index.html" />

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值