Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQM-如何在使用XMLHttpRequest加载页面时获取动态数据?_Javascript_Jquery_Ajax_Jquery Mobile - Fatal编程技术网

Javascript JQM-如何在使用XMLHttpRequest加载页面时获取动态数据?

Javascript JQM-如何在使用XMLHttpRequest加载页面时获取动态数据?,javascript,jquery,ajax,jquery-mobile,Javascript,Jquery,Ajax,Jquery Mobile,我使用pageinit加载页面。我调用XMLHttpRequest,在服务器端使用RESTfulAPI向Linux上的PHP程序发送和获取请求。服务器端接受我发送给它的数组并回显JSON。一切都很好。然而,HTTP_GET函数返回页面并在页面完成并从服务器获得响应之前显示内容 我如何防止它在响应之前返回 pageinit $(document).on("pageinit","#quote_open1",function(){ alert('pageinit quote

我使用pageinit加载页面。我调用XMLHttpRequest,在服务器端使用RESTfulAPI向Linux上的PHP程序发送和获取请求。服务器端接受我发送给它的数组并回显JSON。一切都很好。然而,HTTP_GET函数返回页面并在页面完成并从服务器获得响应之前显示内容

我如何防止它在响应之前返回

pageinit

$(document).on("pageinit","#quote_open1",function(){

             alert('pageinit quote_open1');
             openPage(); });
openPage

function openPage(url, jsontest){
    var jsonResponse=HTTP_GET( url, jsontest);
          alert('!!!!!!!!!!!!!!!!-->'+jsonResponse);
}
HTTP\u-GET

function HTTP_GET( url, jsonToSend){

   alert('START HTTP_GET');

   var jsonResponse;

    //send get request to server
    xhr = new XMLHttpRequest(); 
    xhr.open("GET",url+"jsonToSend",true);  //open server connection                
    xhr.send();//this is where json string will be sent out
   //  
    //this function send 

    xhr.onreadystatechange = function() 
      {
    if (xhr.readyState == 4)  //read server response
            {
            alert(xhr.readyState);
            jsonResponse=xhr.responseText;
            alert("!!!jsonResponse="+jsonResponse);
            alert("!!!xhr.responseText"+xhr.responseText);

            return "server returned a response 1"; //RETURNS this all the time
            }
        };  
   alert('END HTTP_GET');
   if (xhr.readyState == 4) return  "server returned a response 2"; //want to return this when leaves this function
   return "stil in progress returns before it's finished";
};
这3个函数在不同的js中


谢谢。

这里您错过的是xhr是异步的,因此您的函数HTTP\u GET在请求完成之前返回

您可以使用如下回调函数:

  $(document).on("pageinit","#quote_open1",function(){

             console.log('pageinit quote_open1');
             openPage(); });



function openPage(){

var doTheJob = function(jsonResponse){ console.log('jsonResponse : '+jsonResponse); }

    HTTP_GET( url, doTheJob);

}

function HTTP_GET( url,callBackFunc){

   console.log('START HTTP_GET');

   var jsonResponse;

    //send get request to server
    xhr = new XMLHttpRequest(); 
    xhr.open("GET",url,true);  //open server connection                

    xhr.onreadystatechange = function() 
      {
    if (xhr.readyState == 4)  //read server response
            {


            callBackFunc(xhr.responseText);
            }
        }; 
   xhr.send();//this is where json string will be sent out
   //  

   console.log('END HTTP_GET');
};

尝试使用pagebeforeshow而不是PageInit是的,您正在执行一个异步ajax请求,我不建议使用其他方式。此外,为了兼容性,我宁愿使用jQuery ajax调用。我不知道这如何解决问题:pageinit处理程序仍然会在ajax请求完成之前完成。嗨。是的,你是对的,url返回的json将在初始化完成后短暂执行,但我认为这不是op的问题。在他的代码中,url返回的json从未执行过。