2013-10-01 69 views
0

成功的Ajax帖子后,我希望使用JQuery的.load()方法呈现处理程序中与POST关联的模板。 GET请求在POST成功后不断被调用...所以与GET相关的模板得到渲染,而不是与POST关联的模板。感谢您提供的任何提示。ajax post成功后,如何使用JQuery .load()方法发送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" > 

回答

2

当您在时尚而无需额外的数据,你真的只是调用的.get()的简化版本叫​​。如果你试图使用从后返回的数据,你应该做

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

这是不正确的。 'load()'仍然可以发起'POST'请求​​。看到http://api.jquery.com/load/ – haim770

+0

我特别说'当你以这种方式调用.load()而没有额外的数据'。是的,如果您提供了其他参数,您可以发布帖子。 –

+0

你是对的。抱歉。 – haim770

0

你并不需要两个后续请求的响应数据加载到#mentor_list,你可以只需拨打load()一次,并使用POST如下:

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

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

.load()确实允许一个职位,但你必须给它一个数据对象...尝试给它一个新的对象。 ...

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