Question
What will be the output of the following JavaScript code
when executed? let xhr = new XMLHttpRequest(); xhr.open("GET", "https://api.example.com/data", false); xhr.send(); console.log(xhr.responseText);Solution
In this JavaScript code, an XMLHttpRequest object (xhr) is created to send a synchronous GET request to the specified URL. The open method initializes the request, with the third parameter set to false, making the request synchronous. This means that the script will pause execution until the server responds. When xhr.send() is called, the request is sent, and upon completion, xhr.responseText contains the server's response, which is printed to the console. Why Other Options Are Wrong: B) An error due to using a synchronous request: This option is incorrect because while synchronous requests can lead to performance issues (blocking the UI), they do not inherently cause errors unless there are issues with the request itself. C) undefined: This option is incorrect because the responseText will contain the server's response if the request is successful, rather than being undefined. D) The text "GET request was successful": This option is incorrect as it does not reflect a specific response from the server; the actual response depends on the API being called. E) The server's HTML code: This option may be misleading; while it’s possible the server could return HTML, the actual output depends on the response from the specific API being called.
What was the growth percentage in Gross Direct Tax collections for the Financial Year 2023-24 as of March 17, 2024?
Who won gold in the Miss World 2025 Sports Challenge?
Which country launched sustainable development satellite SDGSAT-1 to study Earth from space?
What was the theme of CII Asia Health 2021 summit?
RBI Innovation Hub and which of the following firm has signed a Memorandum of Understanding to give early-stage entrepreneurs with unique and disruptive...
The Government of India announced the winners of the National Startup Awards 2021. 46 startups were felicitated for their contribution in their respecti...
Who is known as ‘Mr Tomboy’ and secured a historic bronze medal for India in eFootball at the 2024 Asian Esports Games?
Which organisation awards the prestigious Equator Prize to recognize community efforts to reduce poverty through the conservation and sustainable use of...
Farhaan Behardien announced his retirement from Cricket. He was the player of which team?
Which of the following was the parent company of Paytm?