Tigerman55
Tigerman55

Reputation: 231

jQuery Ajax POST request

I am trying to get the following code to send variables to a PHP page via POST. I am not quite sure how to do it. This is my code sending data via GET and receiving it via JSON encoding. What do I need to change to pass variables to process_parts.php via POST?

function imagething(){
    var done = false,
    offset = 0,
    limit = 20;
    if(!done) {
        var url = "process_parts.php?offset=" + offset + "&limit=" + limit;

        $.ajax({
            //async: false, defaults to async
            url: url
        }).done(function(response) {

            if (response.processed !== limit) {
                // asked to process 20, only processed <=19 - there aren't any more
                done = true;
            }
            offset += response.processed;
            $("#mybox").html("<span class=\"color_blue\">Processed a total of " + offset + " parts.</span>");
            alert(response.table_row);
            console.log(response);
            imagething(); //<--------------------------recursive call
        }).fail(function(jqXHR, textStatus) {

            $("#mybox").html("Error after processing " + offset + " parts. Error: " + textStatus);

            done = true;
        });
    }
}
imagething();

Upvotes: 1

Views: 128

Answers (2)

cracker
cracker

Reputation: 4906

Try This

         $.ajax({
                    url: "URL",
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ty),
                    dataType: "json",
                    success: function (response) {
                        alert(response);
                    },

                    error: function (x, e) {
                        alert('Failed');
                        alert(x.responseText);
                        alert(x.status);
                    }
                });

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337691

The default method is GET, to change that, use the type parameter. You can also provide your querystring properties as an object so that they are not immediately obvious in the URL:

var url = "process_parts.php";

$.ajax({
    url: url,
    type: 'POST',
    data: {
        offset: offset,
        limit: limit
    } 
}).done(function() {
    // rest of your code...
});

Upvotes: 1

Related Questions