Javascript getting response value after post - javascript

I'm triying to post web service. But i want to get response value.
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);
When i look in firebug, i realized web server response value is:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://tempuri.org/"><CelsiusToFahrenheitResult>111.2</CelsiusToFahrenheitResult> </CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>
But i dont know how can i get
111.2 value?

You are getting an xml response from your ajax call. Do do it correctly, you need to parse the xml code.
You can do easily it with jQuery:
var r = $(this.responseText).find("CelsiusToFahrenheitResult").text();

Related

How to obtain the body response in the client-side?

I am new in web, I am serving an html when a button is clicked on the client side through a request, the body response of that request is the html. How can I retrieve the html or body response from the client side?
I am trying with this code but everything is empty:
var xhr = new XMLHttpRequest();
xhr.open(signedRequest.method, signedRequest.url, true);
console.log('xhr.response: ', xhr.response);
console.log('xhr.responseText: ', xhr.responseText);
console.log('xhr.responseXML: ', xhr.responseXML);
document.write('<p>xhr: ' + xhr + '</p>');
xhr.send();
Any idea on how to obtain the body response in the client-side?
Try to add a div or any input element with the id "demo" and try to run the code below.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = "<strong>The response from the test URL is: </strong>" + this.responseText;
console.log(JSON.parse(this.response));
}
};
xhttp.open("GET", "https://httpbin.org/get", true);
xhttp.send();
<div id="demo"></div>

SOAP webservice client with javascript

I try to write a simple client javascript to access to a SOAP webservice. I found an example on the web
https://thycotic.force.com/support/s/article/Using-Web-Services-with-SOAP-Javascript
and
<!DOCTYPE html>
<head>
<title>SOAP Javascript Test</title>
</head>
<body>
<script type="text/javascript">
function soap() {
var xmlhttp = new XMLHttpRequest();
//replace second argument with the path to your Secret Server webservices
xmlhttp.open('POST', 'https://URL//MANU_WSManutenzioni_MOGE/', true);
//create the SOAP request
//replace username, password (and org + domain, if necessary) with the appropriate info
var strRequest =
'<?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>' +
'<Authenticate xmlns="urn:https://URL">' +
'<IdTipologiaSegnalazione>7</IdTipologiaSegnalazione>' +
'<IdModalitaSegnalazione>6</IdModalitaSegnalazione>' +
'<IdSegnalante>21856</IdSegnalante>' +
'<Descrizione>test</Descrizione>' +
'<IdTipologiaIntervento>21</IdTipologiaIntervento>' +
'<Matricola>emergenze</Matricola>' +
'</Authenticate>' +
'</soap:Body>' +
'</soap:Envelope>';
//specify request headers
//xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xmlhttp.setRequestHeader('Authorization', 'TOKEN');
//FOR TESTING: display results in an alert box once the response is received
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseText);
}
};
//send the SOAP request
xmlhttp.send(strRequest);
};
//build & send the request when the page loads
window.onload = function() {
soap();
};
</script>
</body>
</html>
The client seem to works but I do not understand how to send an authorization token given by the server authority.
I try with
xmlhttp.setRequestHeader('Authorization', 'TOKEN');
but it do not works

Asmx webservice Xmlhttprequest content-type

I have a piece of javascript code for accessing a asmx webservice
var xml = 'xml=<Webservice><Parameters><Parameter name="PARTCODE" value="WSA10029" /></Parameters></Webservice>';
try {
xhr = new XMLHttpRequest();
xhr.open('POST', url, false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
xhr.send(xml);
} catch (e) {
alert('Error occured in XMLHttpRequest: ' + xhr.statusText + ' ReadyState: ' + xhr.readyState + ' Status:' + xhr.status);
}
The webservice receives the request but the parameter xml is empty at the webservice side. i've tried many different Content-Types but none of them work.
Data data I want to send is xml, therefor I htmlencoded the xml.

CRM Calling Actions from Javascript

I'm trying to call a CRM action from javascript and I can't get a response back. I'm not sure what "Target" is supposed to refer to in the action. I don't know if my XML is incorrect or if there is a problem with my url. This is my action in CRM
This is the code that I am using to call the action. I have looked at my examples and I don't know what I am missing.
var request = [];
request.push("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
request.push("<s:Body>");
request.push("<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\"");
request.push(" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">");
request.push("<request i:type=\"b:AssignRequest\"");
request.push(" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\"");
request.push(" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">");
request.push("<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">");
request.push("<a:KeyValuePairOfstringanyType>");
request.push("<c:key>Target</c:key>");
request.push("<c:value i:type=\"a:EntityReference\">");
//request.push("<a:Id>" + this._xmlEncode(Target) + "</a:Id>");
request.push("<a:Id>" + entityId + "</a:Id>");
request.push("<a:LogicalName>prostr_project</a:LogicalName>");
request.push("<a:Name i:nil=\"true\" />");
request.push("</c:value>");
request.push("</a:KeyValuePairOfstringanyType>");
request.push("<a:KeyValuePairOfstringanyType>");
request.push("<c:key>TestString</c:key>");
request.push("<c:value i:type=\"a:string\">");
request.push("test");
request.push("</c:value>");
request.push("</a:KeyValuePairOfstringanyType>");
request.push("<a:KeyValuePairOfstringanyType>");
request.push("<c:key>Count</c:key>");
request.push("<c:value i:type=\"a:int\">");
request.push(1);
request.push("</c:value>");
request.push("</a:KeyValuePairOfstringanyType>");
request.push("</a:Parameters>");
request.push("<a:RequestId i:nil=\"true\" />");
request.push("<a:RequestName>mediastr_ProjectTest</a:RequestName>");
request.push("</request>");
request.push("</Execute>");
request.push("</s:Body>");
request.push("</s:Envelope>");
var clientUrl = Xrm.Page.context.getClientUrl();
var requestUrl = clientUrl + "/XRMServices/2011/Organization.svc/web"; //"/MSCRMServices/2007/CrmService.asmx"; //"/XRMServices/2011/Organization.svc?wsdl";
debugger;
var req = new XMLHttpRequest();
req.open("POST", requestUrl, true)
// Responses will return XML. It isn't possible to return JSON.
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
//req.onreadystatechange = function () { SDK.SOAPSamples.assignResponse(req, successCallback, errorCallback); };
var requestXML = request.join("");
req.send(requestXML);
I would suggest to recheck yourself building Request Xml using SoapLogger. Recheck following article - http://a33ik.blogspot.com/2013/10/custom-actions-walkthrough-for-net-and.html

How to consume soap web service (.asmx) secure by basic authentication using jQuery?

I am trying to call a (.asmx) soap web service that require basic authentication using jQuery (or anything that would work).
And how to pass parameters to the (.asmx) soap web service
I've not been able to come up with any answers on Google. Is it possible?
OK, Finally I was able to resolve this issue :)
I was searching in the wrong direction, I was looking how to open the URL secured by basic authentication using $.Ajax, where I should search for consuming SOAP service from JavaScript using XMLHttpRequest()
the following is the answer to my question:
var symbol = "MSFT";
var xmlhttp = new XMLHttpRequest();
//xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote", true);
// if you use username and password to secure your URL
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote", true, 'username', 'password');
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
console.log(xmlhttp.responseText);
}
}
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);
You need to use jquery ajax for that.
var uri="http://asmx_file_path/asmx_service_file_name/method_name_in_asmx_file"
//ex: var uri = "http://mysite.test.com/services/data/mobile.asmx?method=login&username=" + uname+ "&password=" + pwd;
$.ajax({
type: "GET",
url: uri,
success: function (msg) {
jasondata = eval('(' + msg + ')');
},
});
You also need to add service reference for that asmx file.
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
<asp:ServiceReference Path="~/services/data/mobile.asmx" />
</Services>
</asp:ScriptManagerProxy>
You can have a look at this tutorial.
calling-asmx-web-service-via-jquery-ajax

Categories