简体   繁体   中英

On success of ajax post, how to send a POST request with JQuery .load() method

After a successful Ajax post, I would like the template associated with POST in the handler to be rendered with JQuery's .load() method. The GET request keeps getting called after a successful POST ...so the template associated with GET is getting rendered instead of the one associate with POST. Thanks for any hints you can give.

Javascript:

$(function() {  
    $(".topic_submit").click(function() {  
    var topic = $("#topic").val();
    refresh = 'false'
        $.ajax({  
            type: "POST",  
            url: "/mentorlist",  
            data: {'topic': topic},  
            success: function(dataString) {  
                $('#mentor_list').load('/mentorlist');
                console.log('**mentor_list div updated via ajax.**'); 
            }  
        });  
        return true;  
    });  
}); 

HTML Form:

<form id="topic_search_form"  name="topic_search_form" action="">
 Topic:  <input id ="topic" type="text" name="topic" size="60" placeholder="Search by Keyword" />
<input type="submit" class="topic_submit" name="topic_submit" value="Search" >

When you call .load() in that fashion without additional data you are really just calling a simplified version of .get() . If you're trying to use the data returned from the post, you should be doing

    $.ajax({  
        type: "POST",  
        url: "/mentorlist",  
        data: {'topic': topic},  
        success: function(dataString) {  
            $('#mentor_list').html(dataString);
            console.log('**mentor_list div updated via ajax.**'); 
        }  
    });  

You don't need two subsequent requests to load the response data into your #mentor_list , you can simply call load() once and make is use POST as follows:

$(".topic_submit").click(function()
{
    var topic = $("#topic").val();
    refresh = 'false';

    $('#mentor_list').load('/mentorlist', { topic: topic });
});

.load()确实允许POST,但是您必须给它一个数据对象...尝试给它一个新对象...

$('#mentor_list').load('/mentorlist', {});

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