The content of XMLHttpRequest is accessible via the property response

Typically, when we want to retrieve data using AJAX, we would use code like this:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
        elem.innerHTML = xhr.responseText;          
    }
}

But the question arises - can we obtain the result without setting it as elem.innerHTML?

For example:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
        xhr.responseText;           
    }
}

The challenge I'm facing is that the output of my query is an HTML table generated by PHP and I prefer not to enclose it within additional elements.

Answer №1

One method you may employ is as follows:

elem.innerHTML = xhr.responseText;

However, it's crucial to be cautious as the innerHTML property is restricted to read-only on certain objects such as html, table, tBody, tFoot, tHead, and tr. When altering the innerHTML property, the specified string will entirely substitute the current content of the object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determine whether an object exists within another object and merge its value into the formatted object

When filling out a form, I have a formattedData object that holds a deep copy of the original data object. If a field in the form is changed, I must update the formatted data object with properties from the values object. To simulate this scenario, I crea ...

The 'authorization' property is not available on the 'Request' object

Here is a code snippet to consider: setContext(async (req, { headers }) => { const token = await getToken(config.resources.gatewayApi.scopes) const completeHeader = { headers: { ...headers, authorization: token ...

elimination of nonexistent object

How can I prevent releasing data if two attributes are empty? const fork = [ { from: 'client', msg: null, for: null }, { from: 'client', msg: '2222222222222', for: null }, { from: 'server', msg: 'wqqqqqqqq ...

When the JSON object is transferred to the node server, it undergoes modifications

With the following code snippet, I have managed to develop a function that generates JSON data and transmits it to server.js (my node server). function deleteEmail(i) { emailObj.splice(i, 1); var general = {}; var table = [] general.table ...

Discover the benefits of utilizing router.back() cascade in Next/React and how to effectively bypass anchor links

Currently, I am working on developing my own blog website using Next.js and MD transcriptions. The issue I am facing involves anchor links. All the titles <h1> are being converted into anchor links through the use of the next Link component: <Link ...

Issue encountered when employing the spread operator on objects containing optional properties

To transform initial data into functional data, each with its own type, I need to address the optional names in the initial data. When converting to working data, I assign a default value of '__unknown__' for empty names. Check out this code sni ...

How come my invocation of (mobx) extendObservable isn't causing a re-render?

Can't figure out why the render isn't triggering after running addSimpleProperty in this code. Been staring at it for a while now and think it might have something to do with extendObservable. const {observable, action, extendObservable} = mobx; ...

Loading Bootstrap accordion content with AJAX only when clicked

Is there a way to load ajax content into an accordion only when it is active? This could help prevent unnecessary data loading. It would be helpful to have a spinner displayed while the content is loading. I've searched online but haven't found a ...

Ensuring Proper Sequence of Function Execution in Angular Directive

I'm a bit confused after reading the following two blogs: Blog I: Eric W Green - Toptal Order of execution Compile -> Controller -> PreLink -> PostLink Blog II: Jason More Order of execution Controller -> Compile -> PreLink ...

The issue of drop shadows causing links to not work properly in Internet Explorer

I am currently working on a website design that features a fixed menu positioned behind the body. When the menu icon is clicked, some jQuery code shifts the body to the left. To create the effect of the fixed menu being positioned underneath, I have added ...

Try utilizing multiple URLs to trigger separate AJAX requests with a single click

I am looking to utilize multiple JSON files from different URL APIs simultaneously. Each URL will serve a different purpose - populating table headers with one URL, and table information with two or three URLs. Currently, my code looks like this: $(docum ...

The application of the Same Origin Policy appears to be ambiguous when utilizing the jQuery AJAX

I'm exploring the application of the Same Origin Policy (SOP) in various scenarios. Here is some JavaScript code that I have written in a local HTML file and executed using Chrome on Windows: $(document).ready(function () { $.get("http://www.qu ...

Utilizing JFlex for efficient brace matching

Currently, I am in the process of developing an IntelliJ plugin. One of the key features that I am working on is a brace matcher. After completing the JetBrains plugin tutorial, I was able to get the brace matcher functioning using this regular expression ...

What could be causing my ajax post to be cut short?

After making enhancements to my MVC service with more thorough error handling and logging, I have encountered a persistent error multiple times without being able to reproduce it. Unterminated string. Expected delimiter: ". Path 'Breadcrumbs[18].Para ...

Issue with the functionality of socket.io callback functions

Previously, I used to utilize the socket.io emit callback in the following manner: On the client side: mysocket.emit('helloworld', 'helloworld', function(param){ console.log(param); }); On the server side: var server = r ...

Securing AJAX requests in Laravel for enhanced protection

I'm currently developing a Laravel application that involves numerous AJAX calls. To safeguard the POST and UPDATE calls, I've implemented CSRF tokens in the AJAX headers. However, my concern lies with protecting the GET ajax calls from potentia ...

If there is a value in the array that falls below or exceeds a certain threshold

Imagine you have a set number of values stored in an array and you want to determine if any of them fall above a certain threshold while also being below another limit. How would you achieve this? Provide a solution without resorting to using a for loop o ...

Remove the list by conducting a comparison analysis

<html> <head> <title> Manipulating List Items Using JavaScript </title> <script type="text/javascript"> function appendNewNode(){ if(!document.getElementById) return; var newlisttext = document.changeform.newlist.val ...

Steps to hide a div with jQuery when a user clicks outside of a link or the div itself:1. Create a click event

Here's a simple question - I have a link that when clicked, displays a div. If the user clicks away from the link, the div is hidden, which is good. However, I don't want the div to be hidden if the user clicks on it. I only want the div to disap ...

Why is it that one function does not hold off on execution until Promise.all() is resolved in another function?

I am currently working on a form that allows users to upload files seamlessly. <form @submit.prevent="sendForm" enctype="multipart/form-data"> <input multiple ref="PostFiles" name="PostFiles" type="file" @change="selectFile('add')"> ...