jQuery non-AJAX POST

2019-01-14 01:08发布

问题:

Is there a simple non-AJAX POST method in jQuery?

I am looking for something equivalent to a form on a page with nothing but hidden fields set via JavaScript which then gets POST-ed, causing the browser to load the page set via action. Just a normal POST, but with values set via jQuery.

I suppose I could keep implementing my current method, but I am curious if jQuery provides a quick way. In the background, I imagine it would dynamically create this form with all of the hidden values and submit it.

回答1:

Tidied Darin's excellent solution slightly.

function myFunction(action, method, input) {
    'use strict';
    var form;
    form = $('<form />', {
        action: action,
        method: method,
        style: 'display: none;'
    });
    if (typeof input !== 'undefined' && input !== null) {
        $.each(input, function (name, value) {
            $('<input />', {
                type: 'hidden',
                name: name,
                value: value
            }).appendTo(form);
        });
    }
    form.appendTo('body').submit();
}

This is JSLint-compatible and makes sure that no form is displayed at the end of body tag despite possible css definitions. The usage is also slightly more straightforward, e.g:

myFunction('/path/to/my/site/', 'post', {
    id: 1,
    quote: 'Quidquid Latine dictum sit altum videtur'
});


回答2:

There is nothing built-in. You could create a dynamic form populating it with hidden fields, add it to the DOM and trigger a submit. Here's an example:

function submit(action, method, values) {
    var form = $('<form/>', {
        action: action,
        method: method
    });
    $.each(values, function() {
        form.append($('<input/>', {
            type: 'hidden',
            name: this.name,
            value: this.value
        }));    
    });
    form.appendTo('body').submit();
}

submit('http://www.example.com', 'POST', [
    { name: 'key1', value: 'value1' },
    { name: 'key2', value: 'value2' },
    { name: 'key3', value: 'value3' },
]);


回答3:

I found these answers very useful, and have modified Anssi Herranen's solution to also post arrays to server-side php correctly:

function jqueryPost(action, method, input) {
    "use strict";
    var form;
    form = $('<form />', {
        action: action,
        method: method,
        style: 'display: none;'
    });
    if (typeof input !== 'undefined') {

        $.each(input, function (name, value) {

            if( typeof value === 'object' ) {

                $.each(value, function(objName, objValue) { 

                    $('<input />', {
                        type: 'hidden',
                        name: name + '[]',
                        value: objValue
                    }).appendTo(form);
                } );      
            }
            else {

                $('<input />', {
                    type: 'hidden',
                    name: name,
                    value: value
                }).appendTo(form);
            }
        });
    }
    form.appendTo('body').submit();
}


回答4:

The author asked for a jQuery solution, but this can just as easily be done with plain JavaScript:

function post (action, nameValueObj){
    var form = document.createElement("form");
    var i, input, prop;
    form.method = "post";
    form.action = action;
    for (prop in nameValueObj) { // Loop through properties: name-value pairs
        input = document.createElement("input");
        input.name = prop;
        input.value = nameValueObj[prop];
        input.type = "hidden";
        form.appendChild(input);
    }
    //document.body.appendChild(form); <-- Could be needed by some browsers?
    form.submit();
    return form;
}
// Example usage:
post("actionPage.html", {
    "field1": "hello",
    "field2": "world" 
    /* etc. */
});


回答5:

i think you can use something like:

paramters = {key1: 'value1', key2: 'value2'}

jQuery.ajax({ 
   url:'/your/url', 
   data: jQuery.param(paramters),
   async:false,
   type:'POST',
   success: function(result){
       doSomethingWithYourResult(result);
   }
})

Note the 'asyn=false' setting