Skip to content

Instantly share code, notes, and snippets.

@EtienneR
Created January 7, 2016 23:39
Show Gist options
  • Select an option

  • Save EtienneR/2f3ab345df502bd3d13e to your computer and use it in GitHub Desktop.

Select an option

Save EtienneR/2f3ab345df502bd3d13e to your computer and use it in GitHub Desktop.
XMLHttpRequest RESTful (GET, POST, PUT, DELETE)
// Get all users
var url = "http://localhost:8080/api/v1/users";
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.onload = function () {
var users = JSON.parse(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == "200") {
console.table(users);
} else {
console.error(users);
}
}
xhr.send(null);
// Get a user
var url = "http://localhost:8080/api/v1/users";
var xhr = new XMLHttpRequest()
xhr.open('GET', url+'/1', true)
xhr.onload = function () {
var users = JSON.parse(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == "200") {
console.table(users);
} else {
console.error(users);
}
}
xhr.send(null);
// Post a user
var url = "http://localhost:8080/api/v1/users";
var data = {};
data.firstname = "John";
data.lastname = "Snow";
var json = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = function () {
var users = JSON.parse(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == "201") {
console.table(users);
} else {
console.error(users);
}
}
xhr.send(json);
// Update a user
var url = "http://localhost:8080/api/v1/users";
var data = {};
data.firstname = "John2";
data.lastname = "Snow2";
var json = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open("PUT", url+'/12', true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = function () {
var users = JSON.parse(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == "200") {
console.table(users);
} else {
console.error(users);
}
}
xhr.send(json);
// Delete a user
var url = "http://localhost:8080/api/v1/users";
var xhr = new XMLHttpRequest();
xhr.open("DELETE", url+'/12', true);
xhr.onload = function () {
var users = JSON.parse(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == "200") {
console.table(users);
} else {
console.error(users);
}
}
xhr.send(null);
@devrsantos
Copy link
Copy Markdown

Muito bom, sofri muito até achar..
Obg por compartilhar conosco.

@juulhao
Copy link
Copy Markdown

juulhao commented Aug 8, 2017

caraaaacaaa! era tudo o que eu precisava no momento! <3

@oliveratgithub
Copy link
Copy Markdown

THANK YOU! 👍 👍 👍

@andremxmx
Copy link
Copy Markdown

thanks this helps me so much in my study :)

@anantharamsrikanth
Copy link
Copy Markdown

it's working...

@Quigut
Copy link
Copy Markdown

Quigut commented Feb 20, 2018

Hello,
Id Like to know how to get some numerical values with one API, do some arithmetic ope
rations and with the result modify one of the returned values.
I have done some test but i dont know how to reuse the variables. Please, help me or tell me where can I do some examples.
Thank you.

@heardofdezz
Copy link
Copy Markdown

Hey Etienne i have question if you dont mind, been working on something for awhile and im quite stuck, i have a stackoverflow post has in details if you dont mind taking a look its concerning request on a rails app :) thanks

@heardofdezz
Copy link
Copy Markdown

@iamravimane
Copy link
Copy Markdown

thank 👍

@KailunHuang
Copy link
Copy Markdown

thanks a lot

@elmasbestia
Copy link
Copy Markdown

Merci! Je ne pouvais pas trouver comment envoyer les données pour le PUT / POST (Il arrivait toujours vide au serveur)

@theachyutkadam
Copy link
Copy Markdown

Update request not working for me. It goes to OPTIONS request method.

@ufukomer
Copy link
Copy Markdown

@theachyutkadam PATCH is same for me. Did you find the solution?

@Rahulsekhardas
Copy link
Copy Markdown

Can anyone tell me what sort of file is "users" in the Post requests ?

@k-herwin
Copy link
Copy Markdown

k-herwin commented Mar 7, 2019

Someone found the solution for the Update request ?

@yokristiawan-gmail-com
Copy link
Copy Markdown

At line 44 why 201 instead of 200? Is it a mistake or intentional???

@alfarog1
Copy link
Copy Markdown

alfarog1 commented Jun 9, 2019

Thank you for this!

@BZR4
Copy link
Copy Markdown

BZR4 commented Nov 24, 2019

Muitíssimo Obrigado! Esta muito "precisado" de um exemplo tão prático!

@aacassandra
Copy link
Copy Markdown

Thankyou for this sir..

@HosanaTrabalho2018
Copy link
Copy Markdown

At line 44 why 201 instead of 200? Is it a mistake or intentional???

When um post a file, the answer is 201 (created). It means the file was created successfully in the server.

@yassineuuu
Copy link
Copy Markdown

thank you

@Surbhi-eng
Copy link
Copy Markdown

Surbhi-eng commented Jul 15, 2020

Hi team,
from LN: 31 to 50 i.e. for Post a user, at xhr.onload = function () {} there is asynchronous call so might be control is not reaching at var users = JSON.parse(xhr.responseText); line.
Please guide me here..
Thank You!

Surbhi Rathi

@ashuGulati
Copy link
Copy Markdown

Thanks a lot! It was quite helpful to me:)

@AnichinTaras
Copy link
Copy Markdown

You are the best. God bless you :))

@Risyandi
Copy link
Copy Markdown

Risyandi commented May 2, 2021

Great! thank you.

Copy link
Copy Markdown

ghost commented Jul 30, 2021

Thank you :)

@oC-n
Copy link
Copy Markdown

oC-n commented Dec 20, 2022

This is a great reference. Thanks very much for posting.

@useyourillusions
Copy link
Copy Markdown

Excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment