我正在使用Jquery来实现对文本框的自动完成,我使用过Ajax调用,但是我的ajax网址不起作用。
当我检查它显示此错误时-> POST http://localhost:51444/Searchoperation/searchvalues 404(未找到)
我的HTML和脚本代码
accountEnabled
<script>
$('#myInput').keyup(function (event)
{
var searchname = $('#myInput').val()
$('#myInput').autocomplete(
{
scroll: true,
selectFirst: false,
autoFocus: false,
source: function(request, response)
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Searchoperation/searchvalues",
data: "{'Searchname':'" + searchname + "'}",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
},
error: function (result) { }
});
},
select: function (event, ui) {
var vll = ui.item.val;
var sts = "no";
var url = 'search_app.aspx?prefix=' + searchname; // ur own conditions
$(location).attr('href', url);
}
})
})
</script>
用于从数据库中获取值的代码
<form autocomplete="off">
<div class="search-field" style="width:300px;">-->
<input id="myInput" type="text" name="myCountry" placeholder="SearchName" >
</div>
<input type="submit">
</form>
已读取在文本框中输入的值,但ajax调用不起作用。请帮助
答案 0 :(得分:0)
尝试在路径的开头(“ / Searchoperation / searchvalues”)中使用斜杠:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Searchoperation/searchvalues",
data: "{'Searchname':'" + searchname + "'}",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
},
....
答案 1 :(得分:0)
您缺少页面扩展名(.ASPX)。替换如下所示的ajax属性网址,
url: "Searchoperation.aspx/searchvalues"
此外,请确保在searchvalues方法之前添加了[WebMethod]。
我希望这会有所帮助。