PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] How to make synchronous HTTP GET request in JavaScript with axios?

 October 14, 2022     axios, javascript     No comments   

Issue

I'm trying to do something like this:

const axios = require('axios');
function load() {
  const response = axios.get('https://...');
  return response.data;
}

I can't make my function async. I need it to be declared exactly this way and return the data loaded from the URL via GET request.


Solution

Well you can try using XMLhttprequest:

const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

function syncRequest(url) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, false);
  xhr.send(null);
  return xhr.responseText;
}

(() => {
  const res = syncRequest("https://api.github.com/users/octocat");
})();

It will block until it receives an response

You will need to install the XMLHttprequest package:

https://www.npmjs.com/package/xmlhttprequest



Answered By - bill.gates
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing