Demande de poste JavaScript comme un formulaire

Mots clés : javascriptformspostsubmitjavascript

meilleur 5 Réponses Demande de poste JavaScript comme un formulaire

vote vote

95

/**  * sends a request to the specified url from a form. this will change the window location.  * @param {string} path the path to send the post request to  * @param {object} params the parameters to add to the url  * @param {string} [method=post] the method to use on the form  */  function post(path, params, method='post') {    // The rest of this code assumes you are not using a library.   // It can be made less verbose if you use one.   const form = document.createElement('form');   form.method = method;   form.action = path;    for (const key in params) {     if (params.hasOwnProperty(key)) {       const hiddenField = document.createElement('input');       hiddenField.type = 'hidden';       hiddenField.name = key;       hiddenField.value = params[key];        form.appendChild(hiddenField);     }   }    document.body.appendChild(form);   form.submit(); }  
post('/contact/', {name: 'Johnny Bravo'}); 
vote vote

83

// Post to the provided URL with the specified parameters. function post(path, parameters) {     var form = $('<form></form>');      form.attr("method", "post");     form.attr("action", path);      $.each(parameters, function(key, value) {         var field = $('<input></input>');          field.attr("type", "hidden");         field.attr("name", key);         field.attr("value", value);          form.append(field);     });      // The form needs to be a part of the document in     // order for us to be able to submit it.     $(document.body).append(form);     form.submit(); } 
vote vote

73

document.body.innerHTML += '<form id="dynForm" action="http://example.com/" method="post"><input type="hidden" name="q" value="a"></form>'; document.getElementById("dynForm").submit(); 
vote vote

69

function postToURL(url, values) {     values = values || {};      var form = createElement("form", {action: url,                                       method: "POST",                                       style: "display: none"});     for (var property in values) {         if (values.hasOwnProperty(property)) {             var value = values[property];             if (value instanceof Array) {                 for (var i = 0, l = value.length; i < l; i++) {                     form.appendChild(createElement("input", {type: "hidden",                                                              name: property,                                                              value: value[i]}));                 }             }             else {                 form.appendChild(createElement("input", {type: "hidden",                                                          name: property,                                                          value: value}));             }         }     }     document.body.appendChild(form);     form.submit();     document.body.removeChild(form); } 
vote vote

52

    function post_to_url(path, params, method) {         method = method || "post";          var form = document.createElement("form");          //Move the submit function to another variable         //so that it doesn't get overwritten.         form._submit_function_ = form.submit;          form.setAttribute("method", method);         form.setAttribute("action", path);          for(var key in params) {             var hiddenField = document.createElement("input");             hiddenField.setAttribute("type", "hidden");             hiddenField.setAttribute("name", key);             hiddenField.setAttribute("value", params[key]);              form.appendChild(hiddenField);         }          document.body.appendChild(form);         form._submit_function_(); //Call the renamed function.     }     post_to_url("http://google.com/", { submit: "submit" } ); //Works! 

Questions similaires