Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a JavaScript object from JSON data returned from server
    primarykey
    data
    text
    <p>I need help, I want to create a function which returns an object from an AJAX request, so I can create a new object from data on the server like this:</p> <pre><code>var foo = bar('api.php?x=y'); </code></pre> <p>The function should take a query string as an argument, and the server returns data as JSON.</p> <p>Can you show me how to do this using jQuery, do you have any ideas/examples?</p> <p><em>I will expand on my question because it seems I am doing it all wrong.</em> I am trying to create a web application based on <strong>live data</strong> (stock quotes) so it has to constantly request updated information from the server. When the app is initialized the first step is getting static data from the stocks to be included (such as ticker symbol and issuer), I am thinking the best way to do this is to have a database on the server and an API which does the query and returns the selected stock data as JSON. The next step is to make a request to another API on the server which returns live data (such as price and volume) on the selected stocks, when the complete data is returned it is rendered to html using a template engine. Then the app has to constantly call the second API on some interval to update the live data and render again. Also you should be able to make a new selection of stocks and start the process again.</p> <p>I think the best way to structure an app like this is to have the data inside a custom object which has its own methods to modify the data later (for example sorting and filtering), so if <code>foo</code> is my object with the stock data I can do something like <code>foo.sort()</code>, or something like <code>foo.render()</code> to create the html representation of the data. That is why I thought the best way was to create an object from the data returned by the AJAX call. <em>Could you please tell me if this is the right way to structure an app like this or point me in the right direction?</em></p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload