Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow redirect to external sites if CORS isn't enabled #1891

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/IHP/static/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,15 @@ window.submitForm = function (form, possibleClickedButton) {
'Content-Type',
'application/x-www-form-urlencoded'
);
request.send(parameters.join('&'));

try {
request.send(parameters.join('&'));
} catch (e) {
// If the send method throws an exception, redirect to the form action URL.
// This can happen for example if redirecting to an external site, which doesn't have CORS enabled,
// so we can't use Ajax.
window.location.href = url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for forms that are submitted via POST. I don't think this can be really fixed at all except by disabling the JS form submission in those forms

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll share my example code soon to show my use case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the example: amitaibu/ihp-cms-starter#35

If you navigate to http://localhost:8000 without the PR it will result with console error. With the PR you'll be redirected to the repo of one of the best Haskell frameworks! 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

var buttons = form.getElementsByTagName('button');
Expand Down
Loading