2016-06-28 60 views
0

我之前完成了一些AJAX網站,但從未直接從HTML頁面調用WebService(ASMX)。當我嘗試訪問我的WebService的我不斷收到一條錯誤消息:無法從HTML文件訪問WebService(內部500錯誤)

肥皂:ReceiverSystem.Web.Services.Protocols.SoapException: 服務器無法處理請求。 ---> System.Xml.XmlException: 根級別的數據無效。第1行,位置1. ...

我正在使用目標框架爲4.5的Visual Studio 2015。我知道這是500的內部錯誤,這意味着,這是一個服務器問題。

HTML代碼:

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Testing</title> 
 
\t <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() { 
 
\t  getTotals(); 
 
\t }); 
 

 
\t function getTotals() { 
 
\t  $.ajax({ 
 
      type: "POST", 
 
\t   url: "Services/worker.asmx?HelloWorld", 
 
\t   data: "{}", 
 
\t   contentType: "application/json; charset=utf-8", 
 
\t   dataType: "jsonp", 
 
\t   success: function (data) { 
 
\t    $("#todaysLoad").html(data.d); 
 
\t   }, 
 
\t   error: function (error) { 
 
\t    $("#todaysLoad").html(error.responseText); 
 
\t   } 
 
\t  }); 
 
\t } 
 
    </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"; 
     } 
    } 
} 

的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=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 

我可以成功複製並粘貼WebService URL並在瀏覽器中瀏覽。我甚至可以調用HelloWorld()方法。當我嘗試從我的HTML頁面調用WebService時,我無法訪問它。

我在我的解決方案中安裝了AJAX Control Toolkit(NuGet)。

任何想法?或者,有什麼我失蹤?

+0

你試過將其發送沒有數據? –

+0

是的,我實際上試過使用'data:「」',但仍然沒有運氣 –

+0

只是一個快速更新。我在Visual Studio 2015中創建了一個全新的ASP.NET WebForms項目,在解決方案中導入了JSON,jQuery和AJAX NuGet包。添加了一個WebService,並從我的ASPX Web表單頁面中用JavaScript寫了一個簡單的AJAX調用。結果,我仍然收到SOAP異常錯誤。 –

回答

0

變化:

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

要這樣:

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

上帝保佑你,先生!這是解決方案!一個我仍然不敢相信的錯字導致了這麼多問題。 :-) –

相關問題