0

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

Here is my code

<!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>

where i am wrong How can i achieve this

Any help will be appreciated

4
  • you need to open the url/sendmessage.... with a quote " Commented Sep 15, 2014 at 7:57
  • Can you use jQuery in your project? Commented Sep 15, 2014 at 7:58
  • yes i can use jquery Commented Sep 15, 2014 at 8:04
  • @anil kumar see my answer. Commented Sep 15, 2014 at 8:07

2 Answers 2

8

Read promises and defered.

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

See also jQuery.when()

Sign up to request clarification or add additional context in comments.

1 Comment

Works, very useful!
0

you can do it using async:

<script type="text/javascript" src="https://raw.githubusercontent.com/caolan/async/master/dist/async.js"></script>
<script type="text/javascript">


    var requestUrls = [];//array of urls
        //request all data
async.each(requestUrls, function(url, callback) {
    $.ajax({
        url:url
    }).done(function(data){
        console.log('data',data);
        callback();
    }).fail(function(err){
        console.log('err',err);
        callback(err);
    });
}, function(err) {
    // if any of the file processing produced an error, err would equal that error
    if( err ) {
      // One of the iterations produced an error.
      // All processing will now stop.
      console.log('A url failed to process',err);
    } else {
      console.log('All url have been processed successfully');
    }
});

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.