Can I use a parameter to call loadXMLDoc () from html? (Ajax)

I have an Ajax call. Now I need to parameterize it, since the same Ajax could use all different scripts on the server.

I need

  • run different scripts inxmlhttp.open("GET","/ajaxrun?run=login_build",true);
  • name div in html so that ajax updates the correct td

How to do it in Ajax?

I thought that

  • I would call loadXMLDoc like <button onclick='loadXMLDoc("login_build")' type='button'>and then
  • change function declaration to function loadXMLDoc(script, function()and
  • change the get call to xmlhttp.open("GET","/ajaxrun?run="+script,true);and finally
  • change div to document.getElementById(script).innerHTML=xmlhttp.responseText;

but does nothing

bit of my html

<script type='text/javascript'>
      //<![CDATA[
        function loadXMLDoc()
        {
          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=function()
            {
            document.getElementById("run").innerHTML=xmlhttp.readyState;
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
              document.getElementById("run").innerHTML=xmlhttp.responseText;
              }
            }
          xmlhttp.open("GET","/ajaxrun?run=login_build",true);
          xmlhttp.send();
        }
      //]]>
    </script>
  </head>
  <body>
    <h1>Available test suits</h1>

    <br/><br/>
    <table>
      <tr>
        <td>
          <a href='run?run=login_build'>login_build</a>
        </td>
        <td>
          <button onclick='loadXMLDoc()' type='button'>

            run
          </button>
        </td>
        <td>
          <div id='run'>script results</div>
        </td>
      </tr>
      <tr>
        <td>

          <a href='run?run=login_cycle_build'>login_cycle_build</a>
        </td>
        <td>
          <button onclick='loadXMLDoc()' type='button'>
            run
          </button>
        </td>
        <td>
          <div id='run'>script results</div>

        </td>
      </tr>
0
source share
2 answers

jQuery. , , , XHR.

, , :

function generic(params) {
   if (params.div && params.div instanceof Array && params.div.size() > 0) {
      //multiple divs handling goes here
   }
   ajax(params.url, params.params);
}


generic({
  divs: ['div1','div2'],
  url: 'http://google.com',
  params: {
    param1:'hello',
    param2:'hi'
  }
})
+1

function loadXMLDoc(what_to_run)

<script type='text/javascript'>
  //<![CDATA[
    function loadXMLDoc(what_to_run)
    {
      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=function()
        {
        document.getElementById(what_to_run).innerHTML="<BLINK> processing ...</BLINK>"
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
          document.getElementById(what_to_run).innerHTML=xmlhttp.responseText;
          }
        }
      xmlhttp.open("GET","/ajaxrun?run="+what_to_run,true);
      xmlhttp.send();
    }
  //]]>

<BODY>

    <td>
      <button onclick='loadXMLDoc("login_build")' type='button'>

        run
      </button>
    </td>
    <td>
      <div id='login_build'>script results</div>
    </td>
+1

Source: https://habr.com/ru/post/1769692/


All Articles