Sign in to follow this  

JavaScript + C# WebService error

Recommended Posts

James Rose    38

I am getting an error with the following code, and my ignorance of this subject is an issue.  Any ideas?

var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "http://feelzmecom.webhost4life.com/FeelzMeSRV.asmx/HelloFeelz", false);       
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpReq.send();                     //At this line I get err.message = "undefined"

Share this post


Link to post
Share on other sites
Nefarious Trigger    6,988

Well the webservice itself is erroring, so why would you expect the JavaScript to work?


There is no hellofeelz method

Share this post


Link to post
Share on other sites
virtorio    2,243

At a guess, your browser is probably enforcing the "Same Origin Policy".

 

edit: And yeah, the web site is also broken, but if was working you should be getting an error code, not an exception.

  • Like 1

Share this post


Link to post
Share on other sites
James Rose    38

 

I am getting an error with the following code, and my ignorance of this subject is an issue.  Any ideas?

var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "http://feelzmecom.webhost4life.com/FeelzMeSRV.asmx/HelloFeelz", false);       
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpReq.send();                     //At this line I get err.message = "undefined"

 

"HelloFeez" does exist, see here:  http://feelzmecom.webhost4life.com/FeelzMeSRV.asmx

 

I'm tired and going to sleep, but I will check it with my C# Desktop app, but even the other two services are giving me the same error

Share this post


Link to post
Share on other sites
James Rose    38

UPDATE:

 

The following code works, but the response is showing: Error 404.0 The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

 

I ran a test app from C# and the functions, all three of them work within that, so I know the functions work.  wth am I doing wrong?

 

 

JavaScript code.

xmlHttpReq.open("POST", "http://feelzmecom.webhost4life.com/FeelzMeSRV.asmx/HelloFeelz", false);				
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpReq.send();                         
alert(xmlHttpReq.responseText);

C# Code:

        [WebMethod(CacheDuration = 2, Description = "Just a Test.")]
        public string HelloFeelz()
        {
            return "The time is " + DateTime.Now.ToShortTimeString();
        }

Share this post


Link to post
Share on other sites
James Rose    38

Well, it's been a fun few days.  I got it working, but of course I now have another issue.

 

Working code

function GetFeelz(){ 
	try {
		var sURL = 'Test1';
		var xmlHttpReq = new XMLHttpRequest();
				
		xmlHttpReq.open("POST", "http://feelzmecom.webhost4life.com/FeelzMeSRV.asmx", false);
		xmlHttpReq.setRequestHeader("Content-Type", "application/soap+xml; charset=utf-8");
		xmlHttpReq.send("<?xml version='1.0' encoding='utf-8'?><soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'><soap12:Body><GetFeelz xmlns='http://feelzmecom.webhost4life.com/'><sInput>" + sURL + "</sInput></GetFeelz></soap12:Body></soap12:Envelope>");

		var xmlDoc = xmlHttpReq.responseXML;
		var htmlCollection = xmlDoc.getElementsByTagName("GetFeelzResult");
		sURL = htmlCollection[0].childNodes[0].nodeValue;
		alert("Return Value: " + sURL);
	}
	catch (Err) {
		alert("ERROR!: " + Err.Message + ' at ' + Err.lineNumber);
	}
}

This works fine if I am calling from the same domain... which introduced me to "Access-Control-Allow-Origin"  My Web Service's web.config file now has the following, but I still get an error of "undefined" on this line;  xmlHttpReq.send

 

Any thoughts?

    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*"/>
          <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        </customHeaders>
      </httpProtocol>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.