我之前已经完成了一些AJAX网站,但从未直接从HTML页面调用WebService(ASMX)。当我尝试访问我的WebService时,我不断收到错误消息:
皂:ReceiverSystem.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Xml.XmlException: 根级别的数据无效。第1行,位置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>
&#13;
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";
}
}
}
Web.Config代码:
<?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=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
我可以成功复制并粘贴WebService URL并在浏览器中浏览到它。我甚至可以调用HelloWorld()方法。当我尝试从我的HTML页面调用WebService时,我无法访问它。
我在我的解决方案中安装了AJAX Control Toolkit(NuGet)。
有什么想法吗?或者,我有什么遗失的东西吗?
答案 0 :(得分:0)
变化:
url: "Services/worker.asmx?HelloWorld",
对此:
url: "Services/worker.asmx/HelloWorld",