There is a code

<div id="ddd">id ddd не изменился</div> <div id="fff">id fff не изменился</div> <script> function AjaxGetData (url, statbox, type, data){ ajax({ url:url, statbox:statbox, method:"POST", data: { type: type, data: data, }, success:function(data){ document.getElementById(statbox).innerHTML=data; } }); }; $(document).ready(function() { AjaxGetData ("AjaxGetData.php", "ddd", "ddd", 1); AjaxGetData ("AjaxGetData.php", "fff", "fff", 1); }); </script> 

Well, php (AjaxGetData.php)

 if($_POST){ var_dump($_POST); } 

In <div id="fff"> everything is displayed normally, but in <div id="ddd"> not.

Tell me where I missed.

  • And why not make one request to return data to JSON and simply not distribute the data across your blocks? - Alexander Sizintsev
  • This is just an example. Actually, there will be more functions and all of them will be different, and not just AjaxGetData.php. And they will not always coincide on different pages. It is not clear to me why it does not work that way. - mrx
  • on my local server, I have your code working as needed - Ordman
  • this is strange, I don’t want to on three machines (two are local servers) - mrx
  • What browser and wasps? If it `s not a secret? jsfiddle.net/jzw69zdt Your code with the 1st and 2nd version of jquery works fine. - E_p

2 answers 2

Try synchronous requests with response status checking.

 <script> function AjaxGetData (url, statbox, type, data){ var res = $.ajax({ url:url, statbox:statbox, method:"POST", async: false, data: { type: type, data: data, }, success:function(data){ document.getElementById(statbox).innerHTML=data; } }).status; return res; }; $(document).ready(function() { var status = AjaxGetData ("res.php", "ddd", "ddd", 1); if (status == 200) AjaxGetData ("res.php", "fff", "fff", 1); }); </script> 
     $.ajax({ type: "post", url: "/ajax.php", dataType: 'json', success: function(data){ if(data.succes == 0){ document.getElementById('stat1').innerHTML=data.stat1; document.getElementById('stat2').innerHTML=data.stat2; } } }); 

    ajax.php

     $json_output = array('succes'=>1, 'stat1'=>'53', 'stat3'=>'10'); echo json_encode($json_output); 
    • This is good if the url is always the same. - mrx
    • Well, then you need to put a cycle and sort through all the links in it. - Alexander Sizintsev
    • I don’t understand why the code I described does not work. Can you say what's wrong? - mrx
    • Well, it will not work correctly, try to put a deferral after a request to the first for the sake of interest ... setTimeout (function () {AjaxGetData ("AjaxGetData.php", "fff", "fff", 1);}, 10000); - Alexander Sizintsev
    • I tried, with a pause it works. it's just hard to guess which one to put) on a crutch is more like ... c alert works well between calls)) - mrx