使用ajax请求多个URL [英] request multiple url using ajax

查看:992
本文介绍了使用ajax请求多个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在按钮单击时使用Ajax调用多个URL,但它不起作用

Hi i am trying to call multiple url using ajax on button click but it is not working

这是我的代码

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(cfun)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url/sendmessage.php?user=password=&mobile=7828208357&message=hello dear shakti&sender=&type=3INFORM",true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc("GET","demo.html",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  });
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="myFunction()">Request data</button>
<div id="myDiv">
</div>

</body>
</html>

我错了 我该怎么做到

任何帮助将不胜感激

推荐答案

阅读承诺并推迟了.

var ajax1 = $.ajax({ 
  dataType: "json",
  url: "url",
  async: true,
  success: function(result) {}                     
});


var ajax2 = $.ajax({ 
  dataType: "json",
  url: "url",
  async: true,
  success: function(result) {}  
});

$.when( ajax1 , ajax2  ).done(function( a1, a2 ) {
   // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
   // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
   var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
   if ( /Whip It/.test( data ) ) {
      alert( "We got what we came for!" );
   }
});

另请参见 jQuery.when()

这篇关于使用ajax请求多个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆