Capuchin
Capuchin

Reputation: 3755

How to force jQuery .load() method to use POST

Why isn't my method posting data? According to jquery docs: "Request Method

The POST method is used if data is provided as an object; otherwise, GET is assumed."

var car = {{ car }}
var motorcycle = {{ motorcycle }}    
$('.send_data').on('click', function(e){
        e.preventDefault();
        $('#'+container).load(
            $(this).attr('href'),
            { car: car, motorcycle: motorcycle },
            function(data) {
                $('#'+container).effect('highlight');
            }
        );
    })

Upvotes: 3

Views: 2921

Answers (3)

Justin Ethier
Justin Ethier

Reputation: 134167

The jQuery .load source code is straightforward:

} else if ( params && typeof params === "object" ) {
    type = "POST";
}

Unless car or motorcycle are not being classified as objects, a POST request should have been issued.

Upvotes: 2

Klemen Tusar
Klemen Tusar

Reputation: 9689

Use $.ajax() and define type: 'post'.

Upvotes: 2

Pataar
Pataar

Reputation: 661

Use $.post() instead of $.load()

Upvotes: 7

Related Questions