如何使用ajaxStart显示加载微调器?

时间:2015-07-03 05:42:34

标签: ajax

我有一个使用命令shell_exec运行python脚本的网页。我想要一个加载微调器,'请等待这个页面加载'在python脚本运行时显示的消息类型,然后在完成剩余的echo&HTML以显示之后显示。

我在https://stackoverflow.com/a/68503/4630491找到了一个好的解决方案 但我是ajax的新手,我不知道如何使用该解决方案。我试着做了

<div id="loadingDiv">Please wait while this page loads.</div>
<script>var $loading = $('#loadingDiv').hide();
$(document)
  .ajaxStart(function () {
    $loading.show();
  })
  .ajaxStop(function () {
    $loading.hide();
  });
</script>

但这不起作用。我是否需要调用ajax来执行ajaxStart?我怎么称呼它?我应该将shell_exec包装在ajax代码中吗?

非常感谢。

1 个答案:

答案 0 :(得分:11)

每当要发送Ajax请求时,jQuery都会检查是否还有其他未完成的Ajax请求。如果没有进行,jQuery将触发ajaxStart事件。

有一个如下所示的加载gif图像

Dim webRequest__1 As WebRequest = WebRequest.Create("https://login.twinfield.com/webservices/session.asmx?wsdl")
Dim httpRequest As HttpWebRequest = DirectCast(webRequest__1, HttpWebRequest)

httpRequest.Method = "POST"
httpRequest.ContentType = "application/soap+xml;charset=UTF-8;action='http://www.twinfield.com/Logon'"
httpRequest.Host = "login.twinfield.com"
httpRequest.Headers.Add("SOAPAction:https://login.twinfield.com/webservices/session.asmx")
httpRequest.ProtocolVersion = HttpVersion.Version11
httpRequest.Credentials = CredentialCache.DefaultCredentials

Dim requestStream As Stream = httpRequest.GetRequestStream()
'Create Stream and Complete Request             

Dim streamWriter As New StreamWriter(requestStream, Encoding.ASCII)
' <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:twin='http://www.twinfield.com/'>

Dim soapRequest As New StringBuilder("<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:twin='http://www.twinfield.com/'><soap:Header/><soap:Body><twin:Logon><twin:user>dgf</twin:user> <twin:password>cfg</twin:password><twin:organisation>dfd</twin:organisation></twin:Logon></soap:Body></soap:Envelope>")
'soapRequest.Append(" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" ")
'soapRequest.Append("xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body>")
'soapRequest.Append("<GetMyName xmlns=""http://tempuri.org/""><name>Sam</name></GetMyName>")
'soapRequest.Append("</soap:Body></soap:Envelope>")

streamWriter.Write(soapRequest.ToString())
streamWriter.Close()
'Get the Response    

Dim htttpresponse As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
'Dim wr As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
'DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Dim srd As New StreamReader(htttpresponse.GetResponseStream())
Dim resulXmlFromWebService As String = srd.ReadToEnd()
'Return resulXmlFromWebService

首先隐藏此加载div(因为当ajax请求即将发送时必须显示加载图像。)

           <div id="loading">
                <img src="loading.gif" />  
           </div>

有关详情,请参阅jQuery documentation

我是否需要调用ajax来执行ajaxStart?我怎么称呼它?

是,当您触发ajax请求时,只会自动触发ajaxStart。

对于ajax,jquery有多种方式,下面我给出了load函数。

              <script>
                   var $loading = $('#loading').hide();
                   //Attach the event handler to any element
                   $(document)
                     .ajaxStart(function () {
                        //ajax request went so show the loading image
                         $loading.show();
                     })
                   .ajaxStop(function () {
                       //got response so hide the loading image
                        $loading.hide();
                    });
              </script>

some_file.py输出将插入到具有类名结果的div中。

要触发加载事件,您可以根据需要使用按钮单击或其他任何操作。

               $( ".result" ).load( "some_file.py" );