【问题标题】:Web method throws internal error..parameters match so not sure whyWeb 方法抛出内部错误..参数匹配所以不知道为什么
【发布时间】: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


【解决方案1】:

如果您将 .NET 1.1 用于 Web 服务和应用程序,则该 Web 服务将不知道如何解释 JSON。有很多情况会产生您遇到的错误,但我注意到您遇到的错误发生在一个名为 ReadParameters 的方法中。所以我专注于参数:

var params= '{"ClientID":' + c + '}';

我第一次看过去。我不是 1.1 Web 服务专家,也不是 AJAX 专家,但除非我大错特错,否则对于 1.1 Web 服务,JSON 只是一个字符串。 Web 服务无法解释(或返回)JSON。

所以我会尝试将您的单个参数作为 int 传递:

var params = c;

看看有什么不同。

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 2017-11-03
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多