Commit 2995fed2 authored by Romain Loth's avatar Romain Loth

fix format guessing: extension check preempts headers

parent 298fc126
...@@ -13,11 +13,25 @@ var AjaxSync = function(args) { ...@@ -13,11 +13,25 @@ var AjaxSync = function(args) {
if (isUndef(args.datatype)) args.datatype = 'text' if (isUndef(args.datatype)) args.datatype = 'text'
else if (args.datatype=="jsonp") args.datatype = "json" else if (args.datatype=="jsonp") args.datatype = "json"
// will contain success bool, format and data itself
var Result = [] var Result = []
// format will influence downstream parser choice
var format = null ;
if (TW.conf.debug.logFetchers) if (TW.conf.debug.logFetchers)
console.log("---AjaxSync---", args) console.log("---AjaxSync---", args)
// use file extension to guess expected format (headers are more variable)
if (args.url && args.url.length) {
let extMatch = args.url.match(/\.(gexf|json)$/)
if (extMatch) {
format = extMatch.pop()
if (TW.conf.debug.logFetchers)
console.info(`before AjaxSync(${args.url}): format is ${format}, according to file extension`);
}
}
$.ajax({ $.ajax({
type: args.type, type: args.type,
url: args.url, url: args.url,
...@@ -28,25 +42,27 @@ var AjaxSync = function(args) { ...@@ -28,25 +42,27 @@ var AjaxSync = function(args) {
data: args.data, data: args.data,
contentType: 'application/json', contentType: 'application/json',
success : function(data, textStatus, jqXHR) { success : function(data, textStatus, jqXHR) {
// header checked iff format not apparent from extension
if (format == null) {
var header = jqXHR.getResponseHeader("Content-Type") var header = jqXHR.getResponseHeader("Content-Type")
var format ; if (header &&
if (!header (header == "application/json"
|| header == "application/octet-stream" || header == "text/json")
|| header == "application/xml" ) {
) { format = "json" ;
}
else {
// default parser choice if xml or if undetailed header // default parser choice if xml or if undetailed header
format = "gexf" ; format = "gexf" ;
}
else {
if (TW.conf.debug.logFetchers) if (TW.conf.debug.logFetchers)
console.debug("after AjaxSync("+args.url+") => response header="+header +"not xml => fallback on json"); console.debug("after AjaxSync("+args.url+") => response header="+header +"not json => fallback on xml gexf");
format = "json" ;
} }
Result = { "OK":true , "format":format , "data":data }; }
Result = { "OK":true , "format":format , "data":data };
}, },
error: function(exception) { error: function(exception) {
console.warn('ajax error:', exception, exception.getAllResponseHeaders()) console.warn('ajax error:', exception, exception.getAllResponseHeaders())
Result = { "OK":false , "format":false , "data":exception.status }; Result = { "OK":false , "format":false , "data":exception.status };
} }
}); });
return Result; return Result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment