我有一个带有搜索框的表单。根据用户搜索的内容,我会跳转到特定页面。使用$ .ajax如何跳转到该URL并相应地将数据发送到该URL?
HTML:
<form method = "POST">
<input id = "search"...>
</form>
JS
//When user presses enter submit the form, jump to the url
$("#search").keyup(function(event){
if(event.keyCode == 13){
form.submit();
}
var landingUrl = '/page/subpage/'; //page changes depending on what the user searched for so I can't put <form action = "/page/subpage/">
var data = '...';
$.ajax({
type: "POST",
data: {
data
},
url: (landingUrl),
contentType: "application/x-www-form-urlencoded",
success: function() {
console.log("post data sent");
},
error: function() {
$window.open(landingUrl, "_self");
}
});
form.submit();
});
如果您需要更多信息,请不要犹豫。