Closed dvv closed 12 years ago
hmm... the before send can be added.
?callback=
is a JSONP thing. in which case i'd probably recommend using $.getScript
$.getScript('http://example.com/?callback=jazz');
of course we can work in some magic in $script.js to wrap it in a nice callback so you can just do this:
$.getScript('foo.js', {
handlerName: function () {
// done
}
});
Personally, I find reqwest interface clearer. The most confusing is type
option which has completely different meanings in both flavors.
What if expose reqwest interface under different alias (say $.Ajax
), and make $.ajax
be a wrapper, which should take jQuery options, reduce them to reqwest options and delegate to $.Ajax
, printing a warning to the console?
Not very clear, but should help initial transition, and constant warnings may eventually make people to use reqwest ;)
Hi! Any progress in coping with this issue? They just consider it a stopper: https://github.com/documentcloud/backbone/pull/322#issuecomment-1044158
I agree with @dvv in principle. I like reqwest's syntax better too.
Perhaps instead of a wrapper, there could be a compat mode: reqwest.compat('jQuery')
or something that's set once.
This would bloat reqwest a little, but a few bytes never hurt anyone =D
I came out with this to patch Spine.js:
if (reqwest) {
$ajax = function(params) {
var error, success;
params.method = params.type.toLowerCase();
params.type = params.dataType;
delete params.dataType;
params.headers = {
'Content-Type': params.contentType
};
if (params.success) {
success = params.success;
params.success = function(data) {
return success(data, "success", this);
};
}
if (params.error) {
error = params.error;
}
params.error = function(data) {
return error(data, "error", this);
};
return reqwest(params);
};
} else if (jQuery) {
$ajax = $.ajax;
}
I am sure it's buggy and handles only one use case. But it's quite similar on what RightJS does with his jquerysh plugin.
i'm starting to like the idea of calling compat('jquery')
which invokes a bridge. i'll see if i can have a play with that.
would a patch for a 'dataType' option (per jQuery, basically would allow sending Content-Type: application/json instead of application/x-www-form-urlencoded) be welcomed? This is an adoption-blocker for me.
You can provide a custom Content-Type
as it is, without a dataType
option. This is roughly what a JSON call can look like with Reqwest:
$.ajax({
url: '/happyPlace',
method: 'post',
type: 'json',
data: JSON.stringify(happyData),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: onSuccess,
error: onError
});
Reqwest has sensible defaults for Accept
and Content-Type
but they aren't enforced if you provide your own.
Thanks, but I'm (also) working with a library that expects things to work like jQuery and take a 'dataType' option instead of specifying headers.
Eh, I can override it just fine with something like:
oldajax = $.ajax;
$.ender({ ajax: function(options) {
if (options.dataType === 'json') {
if (! options.headers) { options.headers = {}; }
options.headers['Content-Type'] = 'application/json';
}
oldajax(options)
}});
It's not ideal but it works.
Hi!
Seems there exist some compat issues preventing use of this $.ajax, at first glance:
Reqwest: method, type, headers, headers['Content-Type'], none, none
jQuery : type, dataType, none, contentType, onBeforeSend(), ?callback=
How'd you suggest to normalize?
TIA, --Vladimir