Brandon Wang
Brandon Wang

Reputation: 3951

How do I tell jQuery that an AJAX request was successful?

I'm trying to submit a form with AJAX through jQuery:

$('.submit input').click(function() {return false;});

$("#addcourseform").submit(function(event) {
    event.preventDefault();
    var formcont = $(this).serialize();
    $.post({
        type:"POST",
        url: "<?php echo base_url(); ?>handover/courseadd",
        data: formcont,
        success: function(returned) {
            alert("It worked: "+returned);
        }
    });
});

This above code is wrapped in ready(), returns no errors in the console, and all that good stuff. However, it seems to append [object Object] to the end of the POST url. Since I use CodeIgniter, that throws a 400 Bad Request because it includes disallowed characters in the URL.

How do I get jQuery from adding that?

EDIT POST-FIX:

For those future people reading this and thinking I'm an idiot, I did in fact use the post() syntax wrong.

Upvotes: 2

Views: 176

Answers (3)

aramboi
aramboi

Reputation: 221

$.post is not the same as $.ajax so you have to provide the parameters differently.

Try this:

$.post("<?php echo base_url(); ?>handover/courseadd", 
        $(this).serialize(), 
        function(returned){     
            alert("It worked: " + returned);
        });

Or just replace post with ajax in your current code.

Upvotes: 6

Pekka
Pekka

Reputation: 449813

Edit: This is actually the oldest error one can make in jQuery - you probably forgot to wrap the whole thing in a ready() handler. :)

JSFiddle that works

Upvotes: 4

Niklas
Niklas

Reputation: 30012

You have another syntax error:

var formcont = $(this).serialize());

Remove the last ):

var formcont = $(this).serialize();

Have a look at your console in your browser, it should log these kinds of problems for you to easily spot.

Upvotes: 2

Related Questions