{"id":1383,"date":"2023-01-15T20:49:13","date_gmt":"2023-01-16T04:49:13","guid":{"rendered":"https:\/\/www.myzips.com\/blog\/how-do-i-make-an-http-request-in-javascript\/"},"modified":"2023-01-09T20:51:06","modified_gmt":"2023-01-10T04:51:06","slug":"how-do-i-make-an-http-request-in-javascript","status":"publish","type":"post","link":"https:\/\/www.myzips.com\/blog\/how-do-i-make-an-http-request-in-javascript\/","title":{"rendered":"How do I make an HTTP request in Javascript"},"content":{"rendered":"<p>To make an HTTP request in JavaScript, you can use the XMLHttpRequest object or the more modern fetch function.<\/p>\n<p>Here is an example using XMLHttpRequest to make a GET request to the JSONPlaceholder API to retrieve a list of posts:<\/p>\n<p><code><br \/>\nconst xhr = new XMLHttpRequest();<\/p>\n<p>xhr.responseType = 'json';<\/p>\n<p>xhr.onreadystatechange = function () {<br \/>\n  if (xhr.readyState === XMLHttpRequest.DONE) {<br \/>\n    console.log(xhr.response);<br \/>\n  }<br \/>\n}<\/p>\n<p>xhr.open('GET', 'https:\/\/jsonplaceholder.typicode.com\/posts');<\/p>\n<p>xhr.send();<br \/>\n<\/code><\/p>\n<p>Here is an example using fetch to make the same request:<\/p>\n<p><code><br \/>\nfetch('https:\/\/jsonplaceholder.typicode.com\/posts')<br \/>\n  .then(response => response.json())<br \/>\n  .then(data => console.log(data));<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To make an HTTP request in JavaScript, you can use the XMLHttpRequest object or the more modern fetch function. Here is an example using XMLHttpRequest to make a GET request to the JSONPlaceholder API to retrieve a list of posts: const xhr = new XMLHttpRequest(); xhr.responseType = &#8216;json&#8217;; xhr.onreadystatechange = function () { if (xhr.readyState [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/posts\/1383"}],"collection":[{"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/comments?post=1383"}],"version-history":[{"count":3,"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/posts\/1383\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/posts\/1383\/revisions\/1386"}],"wp:attachment":[{"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/media?parent=1383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/categories?post=1383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myzips.com\/blog\/wp-json\/wp\/v2\/tags?post=1383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}