javascript 在 ajax post 成功后,如何使用 JQuery .load() 方法发送 POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19126740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On success of ajax post, how to send a POST request with JQuery .load() method
提问by Matt Ellis
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.
成功发布 Ajax 后,我希望使用 JQuery 的 .load() 方法呈现处理程序中与 POST 关联的模板。GET 请求在成功 POST 后不断被调用……因此与 GET 关联的模板正在呈现,而不是与 POST 关联的模板。感谢您提供的任何提示。
Javascript:
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:
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" >
回答by Kevin Pei
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
当您.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.**');
}
});
回答by CrazyDart
回答by haim770
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:
您不需要两个后续请求来将响应数据加载到您的 . 中#mentor_list
,您只需调用load()
一次并按POST
如下方式使用 make :
$(".topic_submit").click(function()
{
var topic = $("#topic").val();
refresh = 'false';
$('#mentor_list').load('/mentorlist', { topic: topic });
});