开发者

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 formc开发者_开发问答ont = $(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.


$.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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜