关于ajax post的成功,如何使用JQuery .load()方法发送POST请求

时间:2013-10-01 22:02:11

标签: javascript ajax jquery web-development-server

在成功发布Ajax之后,我希望使用JQuery的.load()方法呈现处理程序中与POST相关联的模板。成功POST后,GET请求不断被调用...因此,与GET相关联的模板将被渲染而不是与POST相关联的模板。感谢您提供的任何提示。

使用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 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" >

3 个答案:

答案 0 :(得分:2)

当您以这种方式拨打.load()而没有其他数据时,您实际上只是在调用.get()的简化版本。如果您正在尝试使用帖子返回的数据,那么您应该这样做

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

答案 1 :(得分:0)

您不需要两个后续请求即可将响应数据加载到#mentor_list,您只需拨打load()一次,然后使用POST,如下所示:

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

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

答案 2 :(得分:0)

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

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