Ninja Coder Ninja Coder - 5 months ago 46
Ajax Question

Cannot Access WebService From HTML File (Internal 500 Error)

I've done a few AJAX websites before, but never calling a WebService (ASMX) straight from an HTML page. When I try to access my WebService I keep getting an error message:


soap:ReceiverSystem.Web.Services.Protocols.SoapException:
Server was unable to process request. ---> System.Xml.XmlException:
Data at the root level is invalid. Line 1, position 1. ...


I'm using Visual Studio 2015 with a target framework of 4.5. I know this is an internal error of 500 which means, it's a server issue.

HTML Code:



<!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 Code:

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 Code:

<?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>


I can successfully copy and paste the WebService URL and browse to it in my browser. I can even Invoke the HelloWorld() method. When I try to call the WebService from my HTML page, I cannot access it.

I did install the AJAX Control Toolkit (NuGet) in my solution.

Any ideas? Or, is there something that I'm missing?

Answer

Change:

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

To this:

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