Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have constructed a very simple web service in C# this is the server code

C#
using MySql.Data;
[WebService(Namespace = "http://www.example.org/", Description = "Example web service in C#")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ScriptService]
	public class ExampleService : System.Web.Services.WebService
	{
        
    	[WebMethod(Description = "Echos back a string")]
		[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

		public string echoString(String testString)
		{
			// An example of a pragma for throwing an exception for invalid input
			// NB: You will still get basic type error handling even if you don't do this!
			// "InputValidationException" is a commonly used pragma for input handling in .NET
			if (testString.Length < 1)
				throw new InputValidationException("The input string must be at least one character");

			return testString; 
		}
       }


When I access ExampleServics.asmx via a Web browser it works fine. However when I try to access it via javascript using the code below i get the following error
Data at the root level is invalid. Line 1, position 1. It is my understanding that this is caused by a malformed statement. Here is the javascript for the Request
C#
var str = 
   'POST /ExampleService.asmx HTTP/1.1 ' +
     'Host: localhost ' +
     'Content-Type: text/xml; charset=utf-8 ' +
     'Content-Length: length ' +
     'SOAPAction: "http://www.example.org/echoString" ' +
     '<?xml version="1.0" encoding="utf-8"?> ' +
     '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> ' +
     '<soap:Body> ' +
     '<echoString xmlns="http://www.example.org/"> ' +
     '<testString>test</testString> ' +
     '</echoString> ' +
     '</soap:Body> ' +
     '</soap:Envelope> ';

        var url = "http://apislot/ExampleService.asmx";
        var soap_request =  makeSoapRequest(url);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", url, true);
         xhr.onreadystatechange = function() {//Call a function when the state changes.
            // Request finished. Do processing here.
        }
       xhr.send(str);


What I have tried:

I have been trying to solve this for days but nothing seems to work. Been all over the internet but all I find is references to a bad request but I can find nothing wrong with the code above. Please help.
Posted
Updated 11-Oct-18 15:40pm
v2
Comments
ZurdoDev 11-Oct-18 15:17pm    
I would suggest using jQuery.ajax() instead.
dbarni 11-Oct-18 18:38pm    
Thanks so much. I switched to jQuery and it works fine now

1 solution

As mentioned in the comments, I would suggest using jQuery's ajax() call. It handles the complexities for you.

Glad to hear you got it working.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900