akre54 / backbone.nativeajax Goto Github PK
View Code? Open in Web Editor NEWA Backbone.Ajax function powered by native XHR methods
License: MIT License
A Backbone.Ajax function powered by native XHR methods
License: MIT License
Why does NativeAjax parse the response based on the provided accept header and only uses content-type
if no custom accept header was specified?
If I provide an accept header like
application/vnd.myformat, application/json;q=0.9
and the server is unable to serve application/vnc.myformat
it will fall back to application/json
.
However, when the response is processed by NativeAjax, it only looks at the accept header and not on the response content type and therefore never parses JSON responses for this accept header.
Furthermore, the code only checks if the accept header starts with application/json
, so if I rearrange my custom header to
application/json;q=0.9, application/vnd.myformat
it would parse the response. AFAIK the order of the MIME types is not relevant for the accept header according to the specification but NativeAjax depends on it.
We recently switched from jQuery to NativeView/NativeAjax. Before the switch I was able to access the HTTP headers through the XHR object in my Backbone parse
method:
parse(response, options) {
if (options.xhr.getResponseHeader('Content-Type').includes('application/vnd.api+json')) {
...
}
}
With NativeAjax, options.xhr
is a Promise which resolves to the data I already have through the response
parameter.
Hence, I have no possibility to access the response headers or did I miss something?
Backbone: 1.2.1
Backbone.NativeAjax: 0.3.2
It looks like with POST the code is just passing the data object to the XHR which isn't actually sending the data to the server. I'm able to workaround this by adding a contentType of "application/x-www-form-urlencoded" and code like the following to the file.
// Stringify POST query params.
if (options.type === 'POST' && typeof options.data === 'object' && options.contentType == 'application/x-www-form-urlencoded') {
var dataToSend = '';
var stringifyKeyValuePair = function(key, value) {
return value == null ? '' :
'&' + encodeURIComponent(key) +
'=' + encodeURIComponent(value);
};
for (var key in options.data) {
dataToSend += stringifyKeyValuePair(key, options.data[key]);
}
options.data = dataToSend.substring(1);
}
Am I doing something wrong here? data is a standard javascript object with key/value pairs.
type: 'POST',
url: 'myapiurl',
data: data,
contentType: 'application/x-www-form-urlencoded',
success: function(results) {
},
error: function(results) {
}
typeof Promise !== null
on line 63 is not a valid check for the existence of Promise. It should be typeof Promise !== 'undefined'
or something more specific to the implementation you are expecting.
Hi,
would you mind creating and publishing a new version of Backbone.NativeAjax so we can use the latest changes.
cheers
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.