Post web service from javascript - javascript

I'm triying to post webservice from javascript. I'am using this test webservice to post. But If I look in firebug I get following exception:
XML parsing error: syntax error Location: moz-nullprincipal:{9e8dc1d9-98d5-48f5-9106-5a19cb9ca7aa} Column: 1, Row: 1:
Reload the page to get source for: http://www.w3schools.com/webservices/tempconv...
^
My code is shown below:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'http://www.w3schools.com/webservices/tempconvert.asmx', true);
// build SOAP request
var sr =
'<soapenv:Envelope' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soapenv:Body>' +
'<CelsiusToFahrenheit xmlns="http://tempuri.org/">' +
'<Celsius>44</Celsius>' +
'</CelsiusToFahrenheit>'+
'</soapenv:Body>' +
'</soapenv:Envelope>';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done use firebug to see response');
}
}
}
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
// send request
// ...
Have you any idea?

I found the problem.
I gave space near soapenv.
So it is look like now:
'<soapenv:Envelope' + ' ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soapenv:Body>' +
'<CelsiusToFahrenheit xmlns="http://tempuri.org/">' +
'<Celsius>44</Celsius>' +
'</CelsiusToFahrenheit>'+
'</soapenv:Body>' +
'</soapenv:Envelope>';

Related

Uncaught ReferenceError (variable is defined and show in console,but show me this error)

I wrote the following code and the ciso variable is defined and its value is displayed when the code is executed, but when the find_cites function is executed, it shows the Uncaught ReferenceError: x is not defined error.
function find_states(ciso){
var request = new XMLHttpRequest();
request.open('GET', 'https://api.countrystatecity.in/v1/countries/' + ciso + '/states');
request.setRequestHeader('xx', 'xx');
request.onreadystatechange = function () {
if (this.readyState === 4) {
// console.log('Status:', this.status);
// console.log('Headers:', this.getAllResponseHeaders());
// console.log('Body:', this.responseText);
var items2 = JSON.parse(request.responseText);
var output2 = "<select name='state' class='form-select' aria-label='Default select example' style='width: 50%;margin-right: 5%;'>";
// document.getElementById("test").innerHTML = items2;
for(var key in items2){
console.log(items2[key]);
output2+='<option onclick="find_cites(' + ciso + ',' + items2[key].iso2 + ')">' + items2[key].name + '</option>';
}
output2+="</select>";
document.getElementById("state").innerHTML = output2;
// console.log(ciso);
}
};
request.send();
};
function find_cites(ciso,siso){
var request = new XMLHttpRequest();
equest.open('GET', 'https://api.countrystatecity.in/v1/countries/' + ciso + '/states/' + siso + '/cities');
request.setRequestHeader('xx', 'xx');
request.onreadystatechange = function () {
if (this.readyState === 4) {
// console.log('Status:', this.status);
// console.log('Headers:', this.getAllResponseHeaders());
// console.log('Body:', this.responseText);
var items2 = JSON.parse(request.responseText);
var output2 = "<select name='city' class='form-select' aria-label='Default select example' style='width: 50%;margin-right: 5%;'>";
// document.getElementById("test").innerHTML = items2;
for(var key in items2){
console.log(items2[key]);
output2+="<option>" + items2[key].name + "</option>";
}
output2+="</select>";
document.getElementById("city").innerHTML = output2;
// console.log(ciso);
}
};
request.send();
};
You've got possible error here:
equest.open('GET', 'https://api.countrystatecity.in/v1/countries/' + ciso + '/states/' + siso + '/cities');
Should be:
request.open('GET', 'https://api.countrystatecity.in/v1/countries/' + ciso + '/states/' + siso + '/cities');
The ciso variable is a string and must be enclosed in quotation marks ('') when entering the function, but this was ignored when passing data to the function, and the error was related to this point.
The following codes are illustrative:
Old code:
output2+='<option onclick="find_cites(' + ciso + ',' + items2[key].iso2 + ')">' + items2[key].name + '</option>';
New code:
output2+='<option onclick="find_cites(' + "'" + ciso + "'" + ',' + "'" + items2[key].iso2 + "'" + ')">' + items2[key].name + '</option>';

XMLHttpRequest. WSDL request. readyState == 4, but status == 0

I try to parse WSDL information from http://rates.kazpost.kz/postratesprodv2/postratesws.wsdl
But when I try it, I have nothing.
xmlhttp.readyState == 4, but xmlhttp.status == 0.
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'http://rates.kazpost.kz/postratesprodv2/postratesws.wsdl', true);
// build SOAP request
var sr =
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pos="http://webservices.kazpost.kz/postratesws">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<pos:GetPostRateRequest>' +
'<pos:GetPostRateInfo>' +
'<pos:SndrCtg>1</pos:SndrCtg>' +
'<pos:Product>P102</pos:Product>' +
'<pos:MailCat>1</pos:MailCat>' +
'<pos:SendMethod>2</pos:SendMethod>' +
'<pos:Weight>750</pos:Weight>' +
'<pos:Dimension>s</pos:Dimension>' +
'<pos:Value>100</pos:Value>' +
'<pos:From>050031</pos:From>' +
'<pos:To>010001</pos:To>' +
'<pos:ToCountry>au</pos:ToCountry>' +
'</pos:GetPostRateInfo>' +
'</pos:GetPostRateRequest>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done. use firebug/console to see network response');
} else {
alert('ERROR. Status: ' + xmlhttp.status + " " + xmlhttp.statusText);
}
}
}
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
// send request
// alert("Ready.");
Seems like you're trying to make http request under https connection, so request is blocked by browser for security reasons.
HTTP Ajax Request via HTTPS Page for more details

Cannot Control if JSON is Undefined in javascript

I'm trying to create a site in which, through the Spotify Web API, I display the information of a certain element (Artist, Track, Album, etc ...).
I was trying to make a check on the url of the profile image of the Artist object as it is not said to be present, but when the console runs it gives me the error "Unexpected token }".
Could someone help me on why?
Below I leave the code:
function ricercaArtista() {
var xhr = new XMLHttpRequest();
var token = document.getElementById("token").innerHTML;
var artist = document.getElementById("artista").value;
artist = artist.replace(" ", "%20");
console.log(artist);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
var result = '';
for (var i = 0; i < response.artists.items.length; i++) {
console.log(response.artists.items[i]);
try { //from here start the control on the image
result += '<div class="panel panel-primary style="background:url(' +
response.tracks.items[i].album.images[1].url + '");"><div class="panel-body">' +
} catch (error) {
result += '<div class="panel panel-primary");"><div class="panel-body">' +
}
'name : ' + response.artists.items[i].name + '<br/>' +
'popularity : ' + response.artists.items[i].popularity + '<br/>' +
'type : ' + response.artists.items[i].type + '<br/>' +
'' + 'Apri su Spotify' + '<br></div></div>';
}
alert
document.getElementById("artists").innerHTML = result;
}
};
xhr.open('GET', "https://api.spotify.com/v1/search?q=" + artist + "&type=artist", true);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.send();
}

Multiple SOAP request using Javascript

All
I am using my SOAP API using java script.
this example explain how to send single soap request using js
var symbol = "MSFT";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseText);
// http://www.terracoder.com convert XML to JSON
var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
// Result text is escaped XML string, convert string to XML object then convert to JSON object
json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text);
}
}
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
var xml = '<?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> ' +
'<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
'<symbol>' + symbol + '</symbol> ' +
'</GetQuote> ' +
'</soap:Body> ' +
'</soap:Envelope>';
xmlhttp.send(xml);
// ...Include Google and Terracoder JS code here...
Now i want to send multiple soap request at a time (mean request more than one envelop).
As long as the third parameter in XMLHttpRequest.open is set to true the call will be asynchronous. So your should just be able to send a new one without much effort. You do need a new XMLHttpRequest object for it to work.
If you want to use the same callback you can just define it as a function and use this to work with the request object.
function soapCallback() {
if (this.readyState == 4) {
alert(this.responseText);
// http://www.terracoder.com convert XML to JSON
var json = XMLObjectifier.xmlToJSON(this.responseXML);
var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
// Result text is escaped XML string, convert string to XML object then convert to JSON object
json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text);
}
}
var symbol = "MSFT";
var xml = '<?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> ' +
'<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
'<symbol>' + symbol + '</symbol> ' +
'</GetQuote> ' +
'</soap:Body> ' +
'</soap:Envelope>';
var xmlhttp1 = new XMLHttpRequest();
xmlhttp1.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp1.onreadystatechange=soapCallback;
xmlhttp1.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp1.setRequestHeader("Content-Type", "text/xml");
xmlhttp1.send(xml);
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp2.onreadystatechange=soapCallback;
xmlhttp2.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp2.setRequestHeader("Content-Type", "text/xml");
xmlhttp2.send(xml);

Error with XSL validation of Javascript

I am getting validation errors such as: "No processing instruction starts with 'xml...." when I try to validate my code before I put it into the 3rd party app I'm using. How do I send a soap message with Javasript that is embedded on an XSL page without getting this error ? Here is the code in question:
<script language="javascript">
function test1(newSymbol){
var symbol = newSymbol;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
// http://www.terracoder.com convert XML to JSON
var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
// Result text is escaped XML string, convert string to XML object then convert to JSON object
json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text + '\n' + 'Company Name: ' + json.Stock[0].Name[0].Text);
document.getElementById('price').innerHTML = json.Stock[0].Last[0].Text;
document.getElementById('name').innerHTML = json.Stock[0].Name[0].Text;
}
else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
alert('Server Issue');
}
}
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
var xml = '<?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> ' +
'<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
'<symbol>' + symbol + '</symbol> ' +
'</GetQuote> ' +
'</soap:Body> ' +
'</soap:Envelope>';
xmlhttp.send(xml);
}
</script>
Im getting the error on the var xml = line right after I declare the soap headers.
Thanks for your help with this! :)
Just remove the XML declaration: it is not necessary in this case, because the version and encoding pseudo attributes specify the default values -- 1.0 and utf-8.

Categories