XMLHttpRequest for connecting to a secure url - javascript

I am using example code which I found here:
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_post2
I want to get a JSON string from an endpoint that requires authentication.
How do I specify my ID / Pass with the request being made to the endpoint ( Is there a HTTP header which I can use which would ask me to authenticate )?
What is a workaround for connecting to the url which might need us to authenticate?
How can I specify to use a SSL certificate for getting response?
< script >
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "demo_post2.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
} < /script>

Related

Using SC.Get() from the JS library since the OAuth token in header update

Since the SC Dev's OAuth Tokens from URL to Header update i can't get the SC.Get() function of the javascript SDK to work, as it appears to pass the OAuth token in the URL rather than a header resulting in a 401 error.
Is this to be expected or am I missing something? Is it possible to set the header through the properties?
I am using this version of the sdk: https://connect.soundcloud.com/sdk/sdk-3.3.2.js
here is the Get code I am using
var route='/me/activities'
var properties = {
limit: 50
};
SC.get(route, properties, function(data) {})
This is what I'm using instead of SC.Get() now
function getStuff(route,callback){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data=JSON.parse(this.responseText)
console.dir(data)
callback(data)
}
};
xhttp.open("GET", "https://api.soundcloud.com"+route+"?limit=200&linked_partitioning=true", true);
xhttp.setRequestHeader("accept", "application/json; charset=utf-8");
xhttp.setRequestHeader("Authorization", "OAuth "+getCookie("SCaccess"));
xhttp.send();
}
Calling it like this
getStuff(route, function (data) {
//do stuff with data
})

How to use Auth0 Hook to call an API endpoint after a user signs up?

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

XmlHttpRequest to nodejs

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).

Post request gets cut off

I have a problem with my post request in javascript.
I have the following code for sending the post request
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
//stuff
}
};
xhttp.open("POST", "/deleteUser", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("username=" + username);
And in my node.js express application i receive it like this
var username = req.body.username;
Now this works perfect with normal characters (numbers and letters). But when i tried
>df)(*&&^%$
as username it gets cut off to
>df)(*
I suspect it is because of the &.
My question: How do i prevent it from getting cut off.
Thanks in advance!
You need to convert the username parameter into a URI safe string, using xhttp.send("username="+encodeURIComponent(username));

How to write http request in javascript?

I know it can be done but i am no good with http request at all. I have a very specific one i need to write. The code is here but i dont know which line to return to my app for the response.
var request = new XMLHttpRequest();
request.open('POST', 'https://v2.api.xapo.com/oauth2/token');
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setRequestHeader('Authorization', 'Basic YTVlMGExMTViYTc1MThjYzphNWUwYTExNWJhNzUxOGNjYTVlMGExMTViYTc1MThjYw==');
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
}
};
var body = "grant_type=client_credentials&redirect_uri=https://myURI.com";
request.send(body);
This is what im trying to do.
curl --include --request POST --header "Content-Type: application/x-www-form-urlencoded" --header "Authorization: Basic MYKEYHERE[auth]" --data-binary "grant_type=client_credentials&redirect_uri=MYREDIRECTURI" 'https://v2.api.xapo.com/oauth2/token'
The api for this is here
please see the following code :
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "demo_get2.asp?fname=Henry&lname=Ford", true);
xhttp.send();
}
first you define an instance of XMLHttpRequest() that will do the http request for you
second the method xhttp.onreadystatechange = function()will be listening to state change that means it will be executed once an http responde is returned
third xhttp.open method will determine your connection config for example here we set a property "POST" then determining the link you want to post to it along with the variables you want to post like this :
enter the link
put ? mark
put the variable name
put =
put the varible value
put & mark and repeat the steps 3 4 5 if you want to post more variable else put nothing .
fourth the method xhttp.send()will start the http request to the server and once the response is gotten the xhttp.onreadystatechange = function()will be called then you can get the content of the response with xhttp.responseText
I hope this example is clear

Categories

Resources