我正在开发asp.net mvc项目。在使用jQuery Ajax的搜索功能中无法直接指向正确的URL
在搜索表单中,我使用jquery ajax发送了关键字,应该转到http://localhost:54088/Home/Search?q=Demo,它将可以正常运行, 相反,它转到http://localhost:54088/?q=Demo 我也尝试在ajax调用中设置完整网址http://localhost:54088/Home/Search?q=Demo,但我没有用
html
<form class="form-inline my-2 my-lg-0" id="SearchForm">
@Html.TextBox("q", null, new { @class = "form-control mr-sm-2", @placeholder
= "Search", id = "searchinput" })
<input type="submit" class="btn btn-outline-danger my-2 my-sm-0"
value="Search" />
</form>
java脚本
$("#SearchForm").submit(function () {
SearchFormjs();
});
function SearchFormjs() {
var searchinput = $("#searchinput").val();
$.ajax({
url: "/Home/Search",
data: searchinput,
type: "GET",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
console.log("send");
},
error: function (errormessage) {
console.log(errormessage);
}
});
}
家庭控制器
public ActionResult Search(string q){
var result = _db.DomesticsMaterialCost.Where(x=>x.ItemName.Contains(q)).ToList();
return View(result);
}
预期结果ajax将此网址称为http://localhost:54088/Home/Search?q=Demo 实际结果ajax将此网址称为http://localhost:54088/?q=Demo,这是错误的
答案 0 :(得分:0)
@using (Html.BeginForm("Search", "Home", FormMethod.Get))
{
@Html.TextBox("q", null, new { @class = "form-control mr-sm-2", @placeholder = "Search", id = "searchinput" })
<input type="submit" class="btn btn-outline-danger my-2 my-sm-0" value="Search" />
}
$("#SearchForm").submit(function () {
e.preventDefault();
// disable button
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
///
}
});
});