Created
October 6, 2016 13:15
-
-
Save AlwxSin/044deaf121bfefa15fe071fe1ab72b86 to your computer and use it in GitHub Desktop.
Werkzeug and ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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