Skip to content

Instantly share code, notes, and snippets.

@AlwxSin
Created October 6, 2016 13:15
Show Gist options
  • Save AlwxSin/044deaf121bfefa15fe071fe1ab72b86 to your computer and use it in GitHub Desktop.
Save AlwxSin/044deaf121bfefa15fe071fe1ab72b86 to your computer and use it in GitHub Desktop.
Werkzeug and ajax
// Not so usefull
// After failed request do another request in new window
var url = 'endpoint-url-of-your-ajax-request-without-query-params'
$.get(
url + '?param1=false'
).fail(function (jqXHR) {
// open debugger in new window
var endpointUrl = url,
debuggerWindow = window.open('', 'werkzeug debugger');
debuggerWindow.document.open();
debuggerWindow.location.href = endpointUrl;
// set href after document.open() instead of before because
// document.open() erases the path from the new window location's href
debuggerWindow.document.write(jqXHR.responseText);
// now script requests in the HTML (i.e., in jqXHR.responseText) will
// be made relative to endpointUrl rather than relative to the root path
// and hence will go through the Flask app
debuggerWindow.document.close();
})
$(window).ajaxError(function(evt, evtData) {
if(evtData && ('responseText' in evtData)) {
var debuggerWindow = window.open('about:blank', 'debuggerWindow');
debuggerWindow.document.open();
debuggerWindow.document.write(evtData.responseText);
debuggerWindow.document.close();
}
});
const werkzeugDebugger = (flaskResponse, url) => {
if (!sameOrigin(url)) return;
if(!confirm('__DEV__: Server Error! Open Werkzeug Debugger?')) return;
window.history.pushState({}, 'Werkzeug Debugger', url);
try {
window.document.open();
window.document.write(flaskResponse);
}
finally {
window.document.close();
}
};
const sameOrigin = url => {
const parser = document.createElement('a');
parser.href = url;
return !parser.origin || parser.origin === window.location.origin;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment