weixin_33713350 2013-01-31 15:45 采纳率: 0%
浏览 84

jQuery Mobile Ajax请求

I am trying to retrieve information from a javascript file in my jQuery mobile website. Ajax is enabled by default, yet when I try xmlHttpRequest.send(), the responseText is the source code for the page rather than a json structure. The initialize() function is run at pageinit, so my thinking is that the json it is retrieving should exist when called. Also, initialize() works fine on the non-mobile variant of the site so I think it has something to do with how JQM handles ajax requests. Thanks in advance for any assistance.

<!DOCTYPE html>
<html>
    <head>
        var xmlHttpRequest;
        var json;

        <script type="text/javascript">
            function initialize()
            {
                xmlHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() :
                                 new ActiveXObject("Msxml2.XMLHTTP");

                if (xmlHttpRequest == null)
                return;

                xmlHttpRequest.open("GET", "pick.js", false);
                xmlHttpRequest.send();
                json = eval('('+ xmlHttpRequest.responseText +')');
            }
        </script>
        ......
    </head>

    <body>
        <div data-role="page" id="map-page"> 
            <script type="text/javascript">
                $('#map-page').live('pageinit',function(){
                    initialize();
                });
            </script>
            .....
        </div>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33743661 2013-01-31 15:51
    关注

    Since you're using jQuery Mobile (and thusly, jQuery), you should consider using jQuery.ajax -- it handles all of the 'hard stuff' like creating XHR object for you.

    For your situation your code would look like this:

    function initialize() {
    
       $.get("pick.js", function(data, status, jqXHR) { 
           //when the call succeeds, do something with the 'data' param
           console.log(data);
       }, "script");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配