【问题标题】:ASP.NET - Call Web Service From User Control ( .Ascx ) Returns Error 500 Internal Server ErrorASP.NET - 从用户控件调用 Web 服务 (.Ascx) 返回错误 500 内部服务器错误
【发布时间】:2012-01-19 13:57:11
【问题描述】:

所以我有一些问题。问题的目标是,当我尝试使用 Ajax 从用户控件调用 Web 服务时,我得到 500 Internal Server Error。

这是我的代码示例:

网络服务.CS

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class BudgetJson : System.Web.Services.WebService {

    public BudgetJson () 
    {
    }



    [WebMethod]
    public static String GetRecordJson()
    {
        return " Hello Master, I'm Json Data ";
    }
}

用户控制 (.Ascx) 文件(Ajax 调用)

$(document).ready(function () {
            $.ajax({
                type: "POST",
                url: "BudgetJson.asmx",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) 
                {
                    alert(msg);
                }
            });
        });

所以,当页面加载并发送请求时,我得到了这样的响应:

soap:ReceiverSystem.Web.Services.Protocols.SoapException: 服务器是 无法处理请求。 ---> System.Xml.XmlException:数据位于 根级别无效。第 1 行,位置 1。在 System.Xml.XmlTextReaderImpl.Throw(异常 e)在 System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlTextReader.Read() 在 System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() 在 System.Xml.XmlReader.MoveToContent() 在 System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() 在 System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() 在 System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() 在 System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage 消息)在 System.Web.Services.Protocols.SoapServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocol.SetContext(类型类型, HttpContext 上下文、HttpRequest 请求、HttpResponse 响应)在 System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型, HttpContext 上下文,HttpRequest 请求,HttpResponse 响应, Boolean& abortProcessing) --- 内部异常堆栈结束 追踪---

如果我将方法名称添加到 url,我得到这样的错误:

未知的网络方法 GetRecordJson。参数名称:methodName 说明:执行过程中发生未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.ArgumentException:未知的网络方法 GetRecordJson。参数名称:方​​法名

任何解决方案?

【问题讨论】:

    标签: asp.net web-services internal-server-error


    【解决方案1】:

    服务器端的一些事情:

    方法不应该是静态的。这只是 ASPX 页面上的“页面方法”的情况。

    其次,您需要使用 [ScriptService] 属性来装饰服务类,以便能够以 JSON 格式与其通信,我假设您可能想要这样做,因为您使用的是 jQuery。

    [ScriptService]
    public class BudgetJson : System.Web.Services.WebService {
      [WebMethod]
      public String GetRecordJson()
      {
        return " Hello Master, I'm Json Data ";
      }
    }
    

    在客户端,您需要在$.ajax() URL 中指定要执行的方法:

    $.ajax({
      type: "POST",
      url: "BudgetJson.asmx/GetRecordJson",
      data: "{}",
      // The charset and dataType aren't necessary.
      contentType: "application/json",
      success: function (msg) {
        alert(msg);
      }
    });
    

    您还可以稍微简化$.ajax() 的用法,如上所示。从服务器发回的标头中的charset isn't necessary and jQuery automatically detects the dataType

    【讨论】:

    • @AlekoGharibashvili 我最初错过了一个客户端问题。更新了我的答案。
    • 还更新了客户端脚本,但没有什么新内容,这段代码在你的机器上有效吗?有一些配置问题吗?如果此代码在您的机器上运行,请分享您的 web.config 文件...
    • @AlekoGharibashvili 配置要求取决于您使用的 ASP.NET 版本。看看这个:encosia.com/…
    • 这个项目在 .net 2.0 上运行。我的 web.config 文件也像您的示例。是否有一些 IIS 问题?
    • @AlekoGharibashvili 您需要在服务器上安装 ASP.NET Ajax 扩展才能使其与 ASP.NET 2.0 一起使用(如我在上一条评论中链接的帖子中所述;做它说,它应该工作)。
    猜你喜欢
    • 2017-10-28
    • 2012-05-05
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多