2013-10-01 64 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', {});