Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>New ways I: <code>fetch</code></strong></p> <p><strong><em>TL;DR</strong> I'd recommend this way as long as you don't have to send synchronous requests or support old browsers.</em></p> <p>A long as your request is asynchronous you can use the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API" rel="noreferrer">Fetch API</a> to send HTTP requests. The fetch API works with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises" rel="noreferrer">promises</a>, which is a nice way to handle asynchronous workflows in JavaScript. With this approach you use <code>fetch()</code> to send a request and <code>ResponseBody.json()</code> to parse the response:</p> <pre><code>fetch(url) .then(function(response) { return response.json(); }) .then(function(jsonResponse) { // do something with jsonResponse }); </code></pre> <p><em>Compatibility: The Fetch API is not supported by IE11 as well as Edge 12 &amp; 13. However, there are <a href="https://github.com/github/fetch" rel="noreferrer">polyfills</a>.</em></p> <p><strong>New ways II: <code>responseType</code></strong></p> <p>As <a href="https://stackoverflow.com/users/834003/londeren">Londeren</a> has written in <a href="https://stackoverflow.com/a/45501836/398844">his answer</a>, newer browsers allow you to use the <code>responseType</code> property to define the expected format of the response. The parsed response data can then be accessed via the <code>response</code> property:</p> <pre><code>var req = new XMLHttpRequest(); req.responseType = 'json'; req.open('GET', url, true); req.onload = function() { var jsonResponse = req.response; // do something with jsonResponse }; req.send(null); </code></pre> <p><em>Compatibility: <code>responseType = 'json'</code> is not supported by IE11.</em></p> <p><strong>The classic way</strong></p> <p>The standard XMLHttpRequest has no <code>responseJSON</code> property, just <code>responseText</code> and <code>responseXML</code>. As long as bitly really responds with some JSON to your request, <code>responseText</code> should contain the JSON code as text, so all you've got to do is to parse it with <code>JSON.parse()</code>:</p> <pre><code>var req = new XMLHttpRequest(); req.overrideMimeType("application/json"); req.open('GET', url, true); req.onload = function() { var jsonResponse = JSON.parse(req.responseText); // do something with jsonResponse }; req.send(null); </code></pre> <p><em>Compatibility: This approach should work with any browser that supports <code>XMLHttpRequest</code> and <code>JSON</code>.</em></p> <p><strong>JSONHttpRequest</strong></p> <p>If you prefer to use <code>responseJSON</code>, but want a more lightweight solution than JQuery, you might want to check out my JSONHttpRequest. It works exactly like a normal XMLHttpRequest, but also provides the <code>responseJSON</code> property. All you have to change in your code would be the first line:</p> <pre><code>var req = new JSONHttpRequest(); </code></pre> <p>JSONHttpRequest also provides functionality to easily send JavaScript objects as JSON. More details and the code can be found here: <a href="http://pixelsvsbytes.com/2011/12/teach-your-xmlhttprequest-some-json/" rel="noreferrer">http://pixelsvsbytes.com/2011/12/teach-your-xmlhttprequest-some-json/</a>.</p> <p><em>Full disclosure: I'm the owner of Pixels|Bytes. I think my script is a good solution to the problem, so I posted it here. Please leave a comment, if you want me to remove the link.</em></p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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