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.
Related
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>
I am having a xmlhttprequest method like this which gets data authenticated from google sign-in successfully and after that I need to post the data to the login view. Using python and django on the server side.
<form name="Login" method="POST" id="loginForm" action="/login">
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
alert("Token*"+id_token); //this gets executed
var xhr = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhr.open('POST', '/login');
xhr.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
console.log('Signed in as: ' + xhr.responseText);
alert("*******Token*"+id_token)
try
{
xhr.send('idtoken=' + id_token);
}
catch (e)
{
alert("*"+e.toString)
console.log('send() error: ' + e.toString());
return false;
}
views.py
in the views i have defined a login method()
def login(request):
if request.method=='POST':
#process the request and send the data to another page with some additonal parameters
role=user_role
email=user_email
fullname=user_name
return render(request, 'ba/dashboard.html', {'role':role,'email':email,'fullname':fullname})
After the post request is completed the page does not get transferred to the dashboard.html page, it stays on the same login page only. I want it to be transferred to the dashboard.html page with the parameters given.
This is the console output no GET request passed after post is completed
[09/Feb/2021 15:53:05] "POST /login HTTP/1.1" 200 4742
Can anyone please tell me how to achieve this? Thanks
This is working for transferring to the different url i had to add this single line
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
**window.location.href="/home"** //I had hoped is there any other way to achieve without using this
}
for passing the parameters i did this from the post method and got in the onreadystatechange as shown above:
return HttpResponse(
json.dumps({'role':user_role,'email':user_email,'fullname':user_name})
)
Is there ANY way to suppress the browser's login prompt on 401 response when using XmlHttpRequest.
var req = new XMLHttpRequest();
req.open("POST", URL, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
//alert("Action called successfully");
}else if(this.status == 401)
{
//
}
else {
var error = JSON.parse(this.response);
alert(error.Message);
}
}
};
If the URL looks like this:
http://www.example.com
You can pass the login credentials like this:
http://username:password#www.example.com/
Or you could try sending them in a header:
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password))
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET','http://example.com/',true);
xmlHTTP.send();
I just keep getting XHR failed loading: GET, can anyone help???
Thanks
If the server your contacting doesn't have CORS set in the header (e.g. access-control-allow-origin: *), you will not be able to make the request. If you don't have access to the server to set a CORS header, you'll need to contact the server from a server you do control and then pass that to the browser (either with a CORS header or not if it's served from the same domain)
But your code works fine. The problem is with your http://example.com/ not returning
http://jsbin.com/zuhobidako/edit?html,js,console,output
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET','https://jsonplaceholder.typicode.com',true);
xmlHTTP.send();
xmlHTTP.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.body.innerHTML =
this.responseText;
}
};
For server status errors, add this to onreadystatechange
if( this.status > 299 && this.readyState == 4) {
console.log('Server Error: ' + xmlHTTP.statusText);
}
For xmlHTTP code errors...
xmlHTTP.onerror = function() {
console.log('xmlHTTP Error', xmlHTTP.responseText)
}
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();