Can you explain the functionality of the Json onLoad method and how can I use it to send a response?

function MakeJsonRequest() {
    JsonRequest.setRequestHeader("Content-type", "application/json");
    JsonRequest.send(Data);     
    JsonRequest.onload = function() {    
      ProcessJsonResponse(this.responseText);      
    }           
}

// The SomeOtherFunction resides in a separate class.

var SomeObject = {
    ProcessJsonResponse: function() {
        return data;
    }
}
  1. What will be the return value of my MakeJsonRequest function?
  2. Is it possible to return the value from the onLoad call of the JsonRequest?

I need to execute some logic in a different class and then return the value through the MakeJsonRequest function

Answer №1

Seems out of place to me. The JsonClient object is expected to be a XHR object in this scenario. However, a XHR object provides an onreadystatechange event to manage a complete transfer by checking the readyState attribute.

The only deviation is a XDomainRequest object from Internet Explorer. That object does possess an .onload property. Currently, your JsonCall() function returns undefined. You should include a callback function to execute. Like this:

function JsonCall( AnotherFunction ) {
    JsonClient.setRequestHeader("Content-type", "application/json");
    JsonClient.send(Data);     
    JsonClient.onload = function() {    
      AnotherFunction(this.responseText);      
    }           
}

and then invoke it like so:

JsonCall( Object.AnotherFunction );

where I trust that Object is merely a placeholder term here.

Answer №2

How does the Json onLoad work exactly?

The Json onLoad function allows you to assign a callback function to it. This function will be executed once the HTTP response has been received.

Is it possible to return a value from the Json onLoad call?

No, it is not possible to directly return a value from the Json onLoad call. This is because event-driven programming operates based on events triggering responses, rather than synchronous execution where values are immediately returned. Instead, any necessary actions with the data should be carried out within the callback function assigned to the onLoad.

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

What is the reason behind TypeScript's lack of support for exporting with decorators?

The issue regarding this problem was addressed on https://github.com/tc39/proposal-decorators/issues/69. Despite this discussion, TypeScript does not currently support it. The following code demonstrates the lack of support: export @deco() class a {}; H ...

updating the row of an html table with elements from a javascript object

I am faced with the task of dynamically adding rows to a table based on the number of elements in my JavaScript object. The object consists of keys and arrays of values. userobject={ ID: [1,2,3] IP_Address: ["12.21.12 ...

Save a PNG file using the HTML5 Canvas when the Submit button is clicked, with the help of PHP

I am looking for assistance with a signature capture form. I came across a standard template on Github that works perfectly, but I wanted to customize it further. However, my Javascript skills are still in the early stages. The current code allows you to c ...

What is the Dojo counterpart for jQuery's .live() function?

Is there a Dojo alternative to jQuery .live() function? http://api.jquery.com/live/ In my search, the only workaround I came across was to disconnect the event handlers in Dojo and then reconnect them when new dynamic content is added to the page. ...

Transform a pandas dataframe into a customized JSON format

I have a pandas dataframe containing transaction data. id txn_id txn_type dr_cust_id dr_cust_acct dr_cust_name dr_cust_type cr_cust_id ... dept bank_user source stped status difference department_sla predicted_sla 0 ...

running into an issue while attempting to utilize socket.io

Currently, I am utilising the socket.io swift client on my Iphone SE. Below is the swift code snippet: let socket = SocketIOClient(socketURL: URL(string: "http://example.com:4000")!, config: [.log(true), .forcePolling(true)]); socket.connect(); ...

Is there a way to create an event listener that responds to a simultaneous click of both mouse buttons?

Despite my extensive research on the Internet, I was unable to find any examples. Interestingly, Vue only supports right and left clicks separately which I find peculiar as it seems like a basic task that can easily be accomplished with plain Javascript. ...

Focus on all of the individual items except for the one that was just selected

Within the function thirdNavFunction, I am seeking a way to specifically target all instances of the .third-nav classes, excluding the one that has been clicked on. This must be accomplished without utilizing jQuery. How can I achieve this? var thirdNav = ...

While running tests on a React project, the `npm test` command is successful, but unfortunately,

I created a new react app using create-react-app and included some basic components and tests. The tests work fine when running 'npm test', but I encounter an 'Unexpected token' error when using Jest to run the tests with imported compo ...

Basic application - angular has not been declared

I'm currently diving into the realm of javascript and angularjs, following along with the tutorials on . After setting up a basic IntelliJ javascript project, I created two essential files: index.html <!DOCTYPE html> <html lang="en"> ...

`A straightforward technique for establishing client-server communication using NodeJS`

Stumbling upon a valuable code snippet on GitHub for enabling straightforward server-client communication in NodeJS has been quite enlightening. Upon making some adjustments, the finalized structure of my code looks like this: The client (Jade + Javascri ...

Traversing a nested JSON structure

Recently, I obtained a JSON list of airports and it is structured like this: Here are some snippets from the array: var airportData = { "00AK": { "icao": "00AK", "iata": "", "name": "Lowell Field", "city": "Anchor Point", "state": "Al ...

Converting a nested JSON object into a specific structure using TypeScript

In the process of developing a React app with TypeScript, I encountered a challenging task involving formatting a complex nested JSON object into a specific structure. const data = { "aaa": { "aa": { "xxx&qu ...

Encountering an undefined array within a click function nested inside a for loop

Being a newbie in this field, I seem to have overlooked a simple detail. The for loop is functioning correctly, but within it, I encounter an issue with an undefined variable. var categories_info = ["history","excellence","art","social","facilities","p ...

Utilizing Embedded JSON with Vue.js

Can you explain how to extract data from embedded JSON? The data is coming from the database in Vue.js and I can see it in the debugger in Google Chrome. There is a one-to-many relationship in the database where one recipe can have many ingredients. Here ...

To begin, formulating a blank state entity containing a collection of key-value pairs with JSON objects as the values. Subsequently, modifying the contents of

I am working on setting up a reactJS state with an empty array to start. When I receive a message in the form of a JSON object, I want to add a key-value pair to the array, using the received message as the value and a specified key. For example: We have ...

Enable the duplication of strings within an array

Below is the HTML code snippet I am working with: <div class="col-sm-8"> <div class="row col-sm-12 pull-right paddingDiv"> <div class="col-sm-12"> <div class="marginBottom pull-right"> <bu ...

Is it possible to retrieve and return multiple lists from a WCF Service?

Recently, I created a WCF service using C# and I've been able to successfully call the service from jQuery to return a list of data. Now, my goal is to return multiple lists. Is this achievable? The current implementation involves passing back the li ...

What is preventing me from accessing a JSON file using another program while my Processing sketch is running?

Currently, I'm using the saveJSONObject command in Processing to write data to a JSON file. The issue arises when I try to access that same JSON file with a different program (MAX/MSP) while my sketch is still running. For some reason, MAX is unable t ...

Implementing Material-UI Autocomplete: How to Include a Starting Value for the startAdornment

I am using autocomplete with multiple selection permission. https://codesandbox.io/s/bold-jackson-dkjmb?file=/src/App.js In the provided example, there are 3 options for cities. How can I manually insert a value in TextField that is automatically selected ...