I'm trying to send a post request to linkedin services from my backend.
exports.GetAccessToken = function (req, res) {
var xhttp = new XMLHttpRequest();
var decoded = jwt.verify(req.query.jwt_token, MariaDB_config.PUB_key);
xhttp.onreadystatechange = function () { // handle request response
if (this.readyState === 4 && this.status === 200) {
console.log("answer : " + this.responseText);
}
};
xhttp.handleError()
// Send a post request
xhttp.open("POST", "https://www.linkedin.com/oauth/v2/accessToken?code=" + decoded.code + "privatestuff", true);
xhttp.send();
}
And I get below error :
TypeError: Cannot read property 'stack' of undefined
This method was working fine until now.
I was using "xhttp.handleError()" wrong, I deleted it and now it works fine.
Related
I'm trying to inject new user into a separate database after they sign up on Auth0. I was told that using hook is sufficient but I'm not sure how to make the call.
I tried
var django_endpoint = 'some/endpoint/';
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("POST", django_endpoint, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send('{"username": user.email}');
cb(null, user, context);
and ran it, but my endpoint is not receiving anything although the result shows 200 on the auth0 test screen
I am trying to create a draft in gmail using google api.
After authorization I am having trouble using POST to send request body. Here is a simplified version of my code.
var token = hash[1].split('=')[1]; // getting token
var body = "some text";
var base64message = Base64.encode(body); //uses base64 library to encode message
var params ={
"message": {
"raw": base64message
}
}
var request = new XMLHttpRequest();
request.onload = function(){
console.log(this.responseText); // parseError
}
request.open('POST','https://www.googleapis.com/gmail/v1/users/me/drafts?access_token='+token,true);
request.send(JSON.stringify(params));
Solved forgot this:
request.setRequestHeader('Content-Type', 'application/json');
Instead of:
request.onload = function(){
console.log(this.responseText); // parseError
}
Use onreadystatechange after which you ask if(this.readyState == 4 && this.status == 200){.
this.readyState == 4 means that the request is finished or processed
this.status == 200 means that it also succeeded.
.onload was added in XMLHttpRequest 2 whereas onreadystatechange has been around since the original spec. .onload is equal only to this.readyState == 4.
So your code will look like this:
var token = hash[1].split('=')[1]; // getting token
var body = "some text";
var base64message = Base64.encode(body); //uses base64 library to encode message
var params ={
"message": {
"raw": base64message
}
};
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
request.open('POST','https://www.googleapis.com/gmail/v1/users/me/drafts?access_token='+token,true);
request.send(JSON.stringify(params));
hi after searching in the net about how to use the long polling in JavaScript I ended up with three ways, they are mentioned here briefly,but they are implemented using JQuery. I am confused which one to use in case that the AJAX request that I send to the server is asynchronous GET request ,and I don't know how many time it could take.
here is an example AJAX request:
function asynchGETRequest(method,url){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("ok");
}
};
xhttp.open(method, url, true);
xhttp.send();
return (xhttp.responseText);
}
var clientFunctions={
getAnswers : function(callback){
var res=asynchGETRequest("GET", "http://localhost:9000/answers");
callback(JSON.stringify(res));
}
}
clientFunctions.getAnswers (function(){
//do some code here after the ajax request is ended
});
can some one guide me please?
I think I found the solution here
function loadFile(sUrl, timeout, callback){
var args = arguments.slice(3);
var xhr = new XMLHttpRequest();
xhr.ontimeout = function () {
console.error("The request for " + url + " timed out.");
};
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback.apply(xhr, args);
} else {
console.error(xhr.statusText);
}
}
};
xhr.open("GET", url, true);
xhr.timeout = timeout;
xhr.send(null);
}
I'm trying to send XMLHttpRequest from client side js to my node server. But nothing is happening. I'm quite new to this stuff. This is my function in javascript.
function sendTokenToServer(token) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// document.getElementById("demo").innerHTML = xhttp.responseText;
console.log(xhttp.responseText);
}
xhttp.open("GET","http://localhost:3000/", true);
xtthp.send();
};
}
And this is my route in node js
app.get('/fcm', function(req, res) {
console.log('here');
res.end('hee');
});
You aren't making a request to the endpoint you created, you are requesting the route: / (which may or may not exist). Change the request to
xhttp.open("GET","http://localhost:3000/fcm", true);
And it should work (assuming your webpage and the server are running on the same port, otherwise you could run into CORS issues).
I'm learning nodejs, and made a simple site to learn to handle POST requests.
Here is my code:
Browser-side:
function sendRequest (params) {
var xhr = new XMLHttpRequest();
var url = 'result';
xhr.open("POST",url,true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
console.log('onreadystatechange');
if(xhr.readyState == 4 && xhr.status == 200){
console.log('Response text:' + xhr.reponseText);
}
}
xhr.send(params);
}
Server-side:
else if (req.url === '/result') {
req.on('data', function (data) {
var params = data.toString().split('&');
var result = calc(params);
console.log(result.toString());
res.writeHead(200,{'Content-Type':'text/plain'});
res.write('<div>'+result.toString()+'</div>');
res.end();
console.log('Response over');
});
}
When i run this,xhr.responseText is undefined, and i'm having trouble understanding where the error is.
Based on the log, node gets the request, the result is correct,and xhr.onreadystatechange also runs(but xhr.responseText is undefined).
There is typing error in your browser side code. You misspelled responseText.
console.log('Response text:' + xhr.responseText);