Need help with SOAP with JavaScript
I have a current function in javascript that is trying to use soap to get some values from a web service. However, I keep getting errors such as "Unterminated String Constant" and "Object Expected"
here is my function:
Code:
<script type="text/javascript">
function test1(){
var oXmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
oXmlHttp.open("POST","http://www.webservicex.net/WeatherForecast.asmx?wsdl",false);
oXmlHttp.setRequestHeader("Content-Type", "text/xml");
oXmlHttp.setRequestHeader("SOAPAction", "http://www.webservicex.net/GetWeatherByZipCode");
oXmlHttp.send(" \
<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> \
<GetWeatherByZipCode xmlns='http://www.webservicex.net'> \
<ZipCode>60609</ZipCode> \
</GetWeatherByZipCode> \
</soap:Body> \
</soap:Envelope> \
");
alert(oXmlHttp.responseText);
}
</script>
Here is the WSDL url:
http://www.webservicex.net/WCF/Servi...ls.aspx?SID=44
Re: Need help with SOAP with JavaScript
Quote:
Originally Posted by
bagznboardz
However, I keep getting errors such as "Unterminated String Constant" and "Object Expected"
That's what you get when you spread a string through 11 line returns. Make sure the string you are sending is valid.
Re: Need help with SOAP with JavaScript
Even when I take the line returns out and combine the string into 2 lines or one variable I still get the same error.
Re: Need help with SOAP with JavaScript
I don't get any error when I put it all on one line.
Code:
oXmlHttp.send("<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><GetWeatherByZipCode xmlns='http://www.webservicex.net'><ZipCode>60609</ZipCode></GetWeatherByZipCode></soap:Body></soap:Envelope>");
Also, why are you not checking the onreadystatechance before alerting the return?
Re: Need help with SOAP with JavaScript
So I was able to fix the code for the unterminated string constant error in a regular test html page. However, the html page is now getting a permission denied error on the oXmlHttp.open method.
Also my production code is still getting a unterminated string constant error.(The production app uses my embedded js as part of its XSL code). For some reason the same code I used in the html page still gives me a untermed string constant error in the XSL page. Im thinking that the app is changing the code for some reason. I validated it as well before hand. I have tried using the " for the quotes, but then it doesnt validate....
Here is my updated code without the onreadystatechange addition you mentioned.
Code:
<script type="text/javascript">
function test1(){
var oXmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
alert('beforeOpen');
netscape.security.PrivilegeManager.enablePrivilege(“UniversalBrowserRead”);
oXmlHttp.open("POST","http://www.webservicex.net/WeatherForecast.asmx?wsdl",true);
alert('beforesetRequestHeader1');
oXmlHttp.setRequestHeader("Content-Type", "text/xml");
alert('beforesetRequestHeader2');
oXmlHttp.setRequestHeader("SOAPAction", "http://www.webservicex.net/GetWeatherByZipCode");
alert('beforeSEND');
oXmlHttp.send("<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> "
+ "<GetWeatherByZipCode xmlns='http://www.webservicex.net'> "
+ "<ZipCode>60609</ZipCode>"
+ "</GetWeatherByZipCode> "
+ "</soap:Body>"
+ "</soap:Envelope> "
+ " \"");
alert(oXmlHttp.responseText);
}
</script>
Thanks for your help on these errors.