Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve plain text JSON, insert into JavaScript
    primarykey
    data
    text
    <p>I have a URL <code>http://myapp.com/get_data</code> that returns an <code>application/json</code> <code>Content-Type</code>. When I browse to that URL, I'd get a plain-text JSON array in my browser window</p> <pre><code>[[key, value], [key, value], [key, value], ...] </code></pre> <p>I also have a JavaScript function that expects data to be in JSON array format</p> <pre><code>function process_data() { var data = // give me more data in JSON array format... } </code></pre> <p>How do I make my JavaScript browse to <code>http://myapp.com/get_data</code> and assign the resulting JSON array into the <code>data</code> variable inside <code>process_data()</code>?</p> <p>I'm new to JavaScript (coming from a Python background) and I would appreciate if you can suggest solutions that use the core JavaScript library. Solutions using other libraries are welcome also, preferably those that are considered best-practice.</p> <h1>UPDATE</h1> <p>It appears I wasn't clear on my question. Let me provide an example from Python. After doing the necessary imports, I can do something like</p> <pre><code>url = "http://myapp.com/get_data" page = urllib2.urlopen(url) page_source = page.read() </code></pre> <p>This time, <code>page_source</code> is already a Python <code>str</code> object that I can easily play with, assign to other variables, etc. If I could mix Python and JavaScript together, for the context of this question, I want to do something like</p> <pre><code>function process_data() { url = "http://myapp.com/get_data" page = urllib2.urlopen(url) page_source = page.read() var data = convert_str_to_JSON(page_source) } </code></pre> <p>Of course that was just an ugly mishmash of a code, but I hope it conveys what I'm trying to get at:</p> <ol> <li>JavaScript will <code>GET</code> a URL.</li> <li>Read the source.</li> <li>Interpret source as JSON.</li> <li>Assign it to a variable.</li> </ol>
    singulars
    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