无法从HTML文件访问WebService(内部500错误)

本文关键字:内部 错误 WebService 访问 HTML 文件 | 更新日期: 2023-09-27 17:52:57

我以前做过一些AJAX网站,但从来没有直接从HTML页面调用WebService (ASMX)。当我尝试访问我的WebService时,我一直得到一个错误消息:

soap: ReceiverSystem.Web.Services.Protocols.SoapException:服务器无法处理请求。——> System.Xml.XmlException:根级别的数据无效。第一行,位置1. ...

我使用的是Visual Studio 2015,目标框架为4.5。我知道这是一个内部错误500,这意味着,这是一个服务器问题。

HTML代码:

<!DOCTYPE html>
<html>
<head>
    <title>Testing</title>
	<meta charset="utf-8" />
    <script type="text/javascript" src="Scripts/jquery-2.2.4.js"></script>
</head>
<body>
    <form>
        <div id="todaysLoad"></div>
    </form>
    <script type="text/javascript">
    $(document).ready(function () {
	    getTotals();
	});
	function getTotals() {
	    $.ajax({
            type: "POST",
	        url: "Services/worker.asmx?HelloWorld",
	        data: "{}",
	        contentType: "application/json; charset=utf-8",
	        dataType: "jsonp",
	        success: function (data) {
	            $("#todaysLoad").html(data.d);
	        },
	        error: function (error) {
	            $("#todaysLoad").html(error.responseText);
	        }
	    });
	}
    </script>
</body>
</html>

WebService代码:

using System.Web.Script.Services;
using System.Web.Services;
namespace MyApp.Services
{
    /// <summary>The Worker WebService will act as a mediator between database and client</summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [ScriptService]
    public class worker : System.Web.Services.WebService
    {
        [WebMethod (EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string HelloWorld()
        {
            return "Today is Tuesday";
        }
    }
}

网络。配置代码:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <pages>
      <controls>
        <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
      </controls>
    </pages>
  </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647"></jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE='&quot;Web'&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

我可以成功地复制并粘贴WebService URL,并在浏览器中浏览它。我甚至可以调用HelloWorld()方法。当我试图从我的HTML页面调用WebService时,我无法访问它。

我确实在我的解决方案中安装了AJAX控制工具包(NuGet)。

任何想法?还是说,我遗漏了什么?

无法从HTML文件访问WebService(内部500错误)

变化:

 url: "Services/worker.asmx?HelloWorld",

:

 url: "Services/worker.asmx/HelloWorld",