从jquery调用web服务
问题描述:
我需要从jQuery调用Web服务,当我把下面的代码击中断点,但它没有到达Web服务......这个代码有什么问题吗?从jquery调用web服务
function searchItems() {
$("#txtSectionName").autocomplete({
source: function (request, response) {
$.ajax({
url: "/DataService.asmx/SearchSections",
data: "{'searchTerm' : '" + $("#txtSectionName").val() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item.Name
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 1
});
}
答
您的数据格式不正确。 jQuery将负责JSON编码。只需传递一个对象:
$.ajax({
url: "/DataService.asmx/SearchSections",
data: {searchTerm: $("#txtSectionName").val() },
dataType: "json",
type: "POST",
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item.Name
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
您发送的数据不是JSON。在JSON中,键和字符串值必须用双引号括起来。 Web服务可能因此无法处理请求。 – 2012-03-22 15:05:05
@OP你可以分享网络服务方法代码... – Rafay 2012-03-22 15:07:40
感谢你的回复Felix Kling和3nigma,现在工作正常....对于延迟 – shanish 2012-04-03 05:45:33