如何使用ajax重定向到具有参数id
和name
的其他控制器操作,但是这些参数没有显示在网址中?
<form class="bisForm" id="bisForm" name="bisForm" method="post">
<div class="contentOne">
{% for index, b in bis|json_decode %}
<input class="button-primary toBis" type="button" value={{ b }} id={{ "bis_"~index }}>
{% endfor %}
</div> </form>
$(document).ready(function(){
$('.toBis').click(function(event){
event.preventDefault();
var bisId = event.currentTarget.id;
var bisName = event.currentTarget.value;
//console.log(bisName.serialize());
$.ajax({
// contentType: 'application/json; charset=utf-8',
type: "post",
url: "/bis/content/",
data: {name: bisName, id: bisId},
//data: "name="+ bisName+"&id="+ bisId,
contentType: 'application/json; charset=utf-8',
//dataType: 'json',
//data: "name="+bisName,
cache: false,
async: false,
error: function(response){
if(!response)
alert("Server Error");
},
success: function(data){
//console.log(data);
//window.location.href = '/bis/content/';
}
});
return false;
});
});
答案 0 :(得分:1)
不要使用AJAX发布数据,只需提交表单即可。在提交表单之前,有一个隐藏字段并为其指定单击的按钮ID和VALUE (还有表单提交数据的action属性)。像,
<form class="bisForm" id="bisForm" name="bisForm" method="post" action="/bis/content/">
<div class="contentOne">
{% for index, b in bis|json_decode %}
<input class="button-primary toBis" type="button" value={{ b }} id={{ "bis_"~index }}>
<input type="hidden" name="clickedItem" id="clickedItem" value="" />
{% endfor %}
</div> </form>
在JavaScript代码中,进行这些更改
$(document).ready(function(){
$('.toBis').click(function(event){
event.preventDefault();
var bisId = event.currentTarget.id;
var bisName = event.currentTarget.value;
$('#clickedItem').attr('value', bisId +'$'+ bisName); // assign value here
$('#bisForm').submit(); // submit the form
return false;
});
});
在您的控制器操作中,获取sbmitted隐藏值,将其拆分为&#39; $&#39;并根据需要使用它们。
[httppost]
public ActionResult content(string clickedItem)
{
string[] clickedItems = clickedItem.split('$');
string id = clickedItems[0].toString();
string value = clickedItems[1].toString();
}
希望它对你有所帮助。
答案 1 :(得分:0)
将此用于成功&#39;你的ajax电话:
success: function(data) {
var strUri = window.location.href;
window.location.href = strUri.replace(window.location.pathname, '/bis/content/');
}
希望它有效。感谢。