简体   繁体   中英

Ajax request with jQuery not working

I haven't developed a long time, I lost a few reflexes.

Can someone help me?

According to Google Chrome, it happens nothing when I press submit :

HTML

<form>
    <label for="title">Title :</label>
        <input type="text" id="title" />
    <label for="comment">Comment :</label>
        <input type="text" id="comment" />
    <label for="project">Project :</label>
        <input type="text" id="project" />

    <input type="submit" value="Submit" />
</form>

JavaScript

$(function(){
    $('form').submit(function(){
        return false;
        $.ajax({
            type: 'POST',
            url: 'http://support.foo.com/insert_bug.aspx',
            data: 'username=mr%20dog&password=dog%3Ddog&short_desc=' + $('#title').val() + '&comment=' + $('#comment').val() + '&projectid=' + $('#project').val(),
            success: function(msg){
                alert(msg);
            }
        })
    });
});

insert_bug.aspx

string username = Request["username"];
string password = Request["password"];
string short_desc = Request["short_desc"];
string comment = Request["comment"];
string projectid_string = Request["projectid"];

If I send the url from my browser, the bug is added.

The problem is that you're returning from the submit event handler before your call to .ajax , which means your AJAX call is never even reached. You could move the return statement to the end of the function body:

$('form').submit(function(){
    //AJAX call...
    return false;
});

Or you could use the preventDefault method of the event object to the same effect:

$('form').submit(function(e){
    e.preventDefault();
    //AJAX call...
});

I'm surprised if anything happens anywhere - you are returning false from teh submit function, before calling the $.ajax function. Try:

$(function(){
    $('form').submit(function(){
        $.ajax({
            type: 'POST',
            url: 'http://support.foo.com/insert_bug.aspx',
            data: 'username=mr%20dog&password=dog%3Ddog&short_desc=' + $('#title').val() + '&comment=' + $('#comment').val() + '&projectid=' + $('#project').val(),
            success: function(msg){
                alert(msg);
            }
        });
        return false;
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM