
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
In 2014, Q was an amazing library pushing promises and helping us all do asyncronous programming better. Now however that Promise
is part of ES6, and widely available natively in modern browsers and Node.js, and that Q has a vastly different API, we should just use the standard Promise
library. Axios does that using the same API inspired from Angular's $http, so I encorage you to migrate to that library. It's great.
jQuery promises have flaws that make them Promises/A+ compliant and they are not going to be fixed. Q also has a lot more functions for promise manipluation and management.
Once you have a good MVC framework, taking a dependency on a 94kb minified (1.11) library just for $.ajax
is alot, expecially when Q is 19k minified (probably half if you remove the node.js specifics). For example, Knockout 3.0 is 45k minified, and includes support all the way back to IE6 - and you can structure your code properly with it instead of creating spaghetti code coupled to the DOM.
Get some JSON:
Q.xhr.get('/status').done(function(resp) {
console.log('status is ' + resp.data)
})
Post some JSON:
Q.xhr.post('/greet', {
say: 'hello'
}).then(function(resp) {
console.log('success!')
}, function(resp) {
console.log('request failed with status' + resp.status)
})
Listen to progress:
var someLargeData = getSomeLargeData();
Q.xhr.post('/processLargeData', someLargeData).progress(function(progress) {
if (progress.upload) {
console.log('Uploaded: '+progress.loaded+' bytes')
} else {
console.log('Downloaded: '+progress.loaded+' bytes')
}
}).then(function(resp) {
console.log('success!')
})
With modern web applications in mind, application/json
is the default mime type.
On the topic of MVC frameworks not needing jQuery, The Angular devs have adopted Q throught, and their http service uses Q. q-xhr is a fork of that, with the following differences:
application/json
. Angular was doing something odd by sniffing all content via regex matching and then converting it to JSON if it matched. Why? Geez people set your Content-Type
correctly already. Not to mention content sniffing leads to security issues.upload
to the ProgressEvent
object.bower install q-xhr
npm install q-xhr
var Q = require('q-xhr')(window.XMLHttpRequest, require('q'))
Q.xhr.get('https://api.github.com/users/nathanboktae/events').then(.....)
Assuming that q-xhr.js
and q.js
are in your baseUrl
require(['q-xhr'], function(Q) {
Q.xhr.get('https://api.github.com/users/nathanboktae/events').then(.....)
})
<script src="q.js"></script>
<script src="q-xhr.js"></script>
<script>
Q.xhr.get('https://api.github.com/users/nathanboktae/events').then(.....)
</script>
A cache object can be provided either as defaults on Q.xhr.defaults
or per-request (with the per-request object preferred). The object must have a get
function that returns the response object for a url, and a put
function given the url and response object to save it. The most trival implementation would be this:
var cache = {}
Q.xhr.defaults.cache = {
get: function(url) {
return cache[url]
},
put: function(url, response) {
cache[url] = response
}
}
Unlike $http
, q-xhr
will not put pending requests in the cache - only successful responses, and before transforms are applied (they will be re-applied each retrieval).
Assigning a handler to xhr.upload.onprogress
in Chrome causes it to issue a preflight request which requires additional handling on the part of the server. If you don't track upload progress and want to avoid this incompatibility, add option disableUploadProgress: true
to your q-xhr options.
FAQs
XMLHttpRequest (ajax) using powerful Q promises
The npm package q-xhr receives a total of 330 weekly downloads. As such, q-xhr popularity was classified as not popular.
We found that q-xhr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.