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");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面