Jump to content



Photo

JavaScript + C# WebService error

javascript webservice

  • Please log in to reply
5 replies to this topic

#1 James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 08 June 2014 - 23:37

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"



#2 +Nik L

Nik L

    Where's my pants?

  • Tech Issues Solved: 2
  • Joined: 14-January 03

Posted 09 June 2014 - 00:09

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


There is no hellofeelz method



#3 virtorio

virtorio

    Neowinian Senior

  • Tech Issues Solved: 14
  • Joined: 28-April 03
  • Location: New Zealand
  • OS: OSX 10.10, Windows 8.1
  • Phone: LG G3

Posted 09 June 2014 - 00:10

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.



#4 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 09 June 2014 - 03:15

 

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.we...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



#5 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 09 June 2014 - 18:04

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();
        }



#6 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 10 June 2014 - 19:13

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>