Ninja Coder Ninja Coder - 1 year ago 87
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:

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>
<meta charset="utf-8" />
<script type="text/javascript" src="Scripts/jquery-2.2.4.js"></script>
<div id="todaysLoad"></div>

<script type="text/javascript">
$(document).ready(function () {

function getTotals() {
type: "POST",
url: "Services/worker.asmx?HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function (data) {
error: function (error) {

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 = "")]
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
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
<jsonSerialization maxJsonLength="2147483647"></jsonSerialization>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=, 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=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

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 Source


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

To this:

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