【发布时间】:2013-02-18 16:00:49
【问题描述】:
非常老的 1.1 vb.net / asp.net 网络应用程序。我正在尝试使用 ajax 调用来填充自动完成文本框:
$("#ucAddActionItemIssueActions_txtActionItem")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function (request, response) {
//get client value
var c = $("#ucAddActionItemIssueActions_ddlClientAssignTo").val();
var params= '{"ClientID":' + c + '}';
$.ajax({
url: "GetLogins.asmx/GetLogins",
data: params,
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);
}
});},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
var email = GetEmail(ui.item.value);
email = email + ";";
emails.push(email);
$("#ucAddActionItemIssueActions_hdnEmails").val(emails.join(""));
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join("");
return false;
}
});
web 方法(.asmx 文件)是这样的(仅作为测试用例):
Imports System.Web.Services
Imports System.Collections
<System.Web.Services.WebService(Namespace := "http://tempuri.org/quikfix.jakah.com/GetLogins")> _
Public Class GetLogins
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetLogins(ByVal ClientID As Integer) As String()
Dim myList As New ArrayList
myList.Add("jstevens")
myList.Add("jdoe")
myList.Add("smartin")
Dim arr() As String = CType(myList.ToArray(Type.GetType("System.String")), String())
Return arr
End Function
End Class
在 chrome 的开发者工具中运行我的应用程序时,它会引发内部 500 错误。当我点击它时,它会说:
System.InvalidOperationException:请求格式无效: 应用程序/json;字符集=UTF-8。在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 在 System.Web.Services.Protocols.WebServiceHandler.Invoke() 在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
参数都匹配,所以我不确定为什么会抛出错误。我是否需要在我的web.config 文件中明确提供对我的 .asmx 文件的任何引用?这是一个旧的 1.1 .net 网络应用程序,所以我不确定是否需要对 web.config 文件进行任何更改?
【问题讨论】:
-
var params= '{"ClientID":"' + c + '"}';将c括在引号中作为字符串,但您的代码隐藏方法需要Integer。改用var params= '{"ClientID":' + c + '}';(c周围没有引号)。 -
我不是 Ajax 专家,但我相当肯定 1.1 早于 JSON 的广泛采用。也许
application/json不是它识别的请求格式。 -
您还在使用 1.1 .NET 版本吗?查看这篇文章:encosia.com/…
-
@AnnL。您可能是对的,我需要将其更改为什么格式/内容类型?
contentType: "application/json; charset=utf-8", -
@oJM86o 好问题,我不确定,因为在 .NET 2.0 之前我没有做 Ajax。 1.1 Web 服务 (IIRC) 的默认交付是 xml,所以我认为“application/xml; charset=utf-8”。但是,如果您打算在从 Web 服务返回结果之前使用 3rd 方库对结果进行 JSON 化,那么它可能是“application/text; charset=utf-8”,因为您将返回一个字符串。
标签: javascript jquery asp.net