How is it possible and is it even possible to realize sending a POST request without reloading the page without connecting Ajax and jQuery?

  • 7
    Would understand first what is AJAX and what is jQuery ... And then the question is like the meaning of "Is it possible to dig a hole without using the digging of the pit and the bulldozer?" - Indifferent
  • Well, there was a time when there was no Ajax or jQuery, but stronys worked without rebooting! - Kirpich643
  • one
    Aha, when the sites were one-page) - t0di
  • Sites began to work without rebooting only with the advent of Ajax? - Kirpich643
  • @ Kirpich643, take a look here . It can all read and not necessary, but a couple of paragraphs in the "History" - read. - Deonis

6 answers 6

<iframe name="iframe1" style="position: absolute; left: -9999px;"></iframe> <form method="POST" action="/handler/" target="iframe1">...</form> 
  • 3
    Just do not have to hide in this way. Enough display: none; - Deonis
  • I don't quite understand what action = "/ handler /" means and what does it mean for the form target = "iframe1"? and if it is possible an example: suppose that there is a form <form action = "handler.php"> <input type = "text" name = "name"> <input type = "submit"> </ form> how to send its contents to the server without rebooting stronitsi and get an answer? - Kirpich643
  • <iframe id = "iframe1" name = "iframe1" style = "position: absolute; left: -9999px;"> </ iframe> <form action = "handler.php" target = "iframe1"> <input type = " text "name =" name "> <input type =" submit "> </ form> // trying to reach the iframe var fr = document.getElementById ('iframe1'); var fw = (fr.contentWindow || fr.contentDocument); if (!!! fw.document) fw = fw.defaultView; if (fw) // fw is the window object of the frame // break it completely. As you want (almost) - istem
  • And that means: var fw = (fr.contentWindow || fr.contentDocument); if (!!! fw.document) fw = fw.defaultView; - Kirpich643
  • This is an attempt to access the internals of the frame. w3schools.com/jsref/prop_frame_contentwindow.asp - istem

There is another fun way to do ajax without, actually, ajax. The method consists in creating the <script> tag in the document header:

 // Загружает и выполняет переданный JavaScript по ссылке function load_js(js_url, js_id) { if (js_id == undefined) js_id = 'some_js_script'; var script = document.createElement('script'); script.id = js_id; script.type = "text/javascript"; uniq_string = js_id + cdt_uniq_string(); if (js_url.indexOf("?") > 0) delim = '&'; else delim = '?'; script.src = js_url + delim + uniq_string; document.getElementsByTagName("head")[0].appendChild(script); }; // Рандомная строка, используется для добавления к ссылке на JS, чтобы браузер не кешировал ее function cdt_uniq_string() { return Math.random().toString().replace(/\./g,""); }; def some_callback(arguments) { alert(arguments) } 

The point is that the server, along with the javascript request, passes the necessary parameters in the link, it somehow processes them and returns a valid javascript as a string like:

 some_callback('string from server!'); 

As a result, the browser sends some parameters to the server and in return receives some data in the function some_callback, which is what is needed from the Ajax. This method is used, for example, for a cross domain Ajax.

    Ajax First, you need to collect data from the form, and secondly, send this form by post-request. The most convenient way is to use jQuery for this, but it is not necessary to do this, you can write all the code you need for such a request yourself ( google : AJAX requests for native JS ).

    The second option is to place the form in an iframe , then, when you click on a submit, only the iframe will reload, and not the whole page.

      Not connecting Ajax and jQuery? - About jQuery, okay, but why connect Ajax? JS without any libraries allows you to use Ajax. There are only some difficulties for cross-browser compatibility, but it is easy to google, the ready code is 10 lines there. Well, you can write it yourself. And use it without connecting anything at all.

        It only occurs to me that I periodically send requests from the Ajax client to the server. But I think that it is possible to make pushing from the server using sockets

          Here I recently wrote the code but here GET

           function jsonp(url) { var head = document.head; var script = document.createElement("script"); script.setAttribute("src", url); head.appendChild(script); head.removeChild(script); } function jsonpCallbac(data) { console.log(data); } sendData.addEventListener("click", function() { var inputElements = form.getElementsByTagName('input'); formData = '?callback=jsonpCallbac'; for (var i = 0; i < inputElements.length; i++) { var inputData = inputElements[i]; if(inputData.value.length > 0){ formData += '&' + inputData.name + '=' + inputData.value; } } jsonp(url + formData); }); 

          Php

           <?php header("Content-Type: application/javascript"); $callback = $_GET["callback"]; $phone = $_GET["phone"]; $json = "{\"status\":\"" . true . "\"}"; echo $callback . "(" . $json . ")"; ?> 
          • To alter your solution from GET to POST is impossible, unfortunately. Moreover, you use JSONP, which also greatly simplifies everything. - Pavel Mayorov

          Protected by spirit community member Apr 22 '16 at 16:30 .

          Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

          Maybe you want to answer one of the unanswered questions ?