0

I'm working with pages like http://musicbrainz.org/release/72cf3c87-d97d-49cc-8781-661ad3580091/cover-art-uploader?id=17477511090 . This is generally an iframe's source, but I'm using it outside of the iframe, in a userscript. I've been testing submitting the form on that page with javascript, but I do not want the redirect after submission.

Here's the js I've been running from the console:

jQuery(document.forms[0]).submit(function (e) {
    e.preventDefault();
    alert('foo');
    return false;
})[0].submit();

From everything I know, it ought to submit, then the event handler kick in to stop the redirect. However, when the server gives anything other than a 200 status, such as 500 Internal Server Error, I still get the redirect.

So 1) How can I detect the error, and its status code, and 2) how can I prevent the redirect, even when the result is an error?

Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
BrianFreud
  • 7,094
  • 6
  • 33
  • 50
  • afaik the form's submission action occurs via the redirect; preventing default/returning false stops the submission (by stopping the redirect). To make an http request without the browser displaying a new page, you need an ajax solution. See [jQuery .ajax](http://api.jquery.com/ajax/). – nbrooks Aug 15 '12 at 05:59
  • That's not going to work, because it'll trigger cross-domain limits. – BrianFreud Aug 15 '12 at 06:05
  • For the search form? It seems to be on the same domain. See the example I included – nbrooks Aug 15 '12 at 06:21
  • No, the submit URL is in the action attrib; it's submitting to s3. – BrianFreud Aug 15 '12 at 11:35

1 Answers1

0

With just plain submit(), this doesn't look promising.

HOWEVER, you can do it. Just use XHR's .send() on a FormData created from the form, rather than trying to submit the form directly. Unlike .submit(), .send() won't cause document.location to follow the redirect. Instead it will just follow the redirect in the GET for the arguments passed to the XHR callback, without touching document.location.

For example code for creating the FormData, see Rob W's answer to Grab file with chrome extension before upload .

For the cross-domain .send(), see the bottom half of the question at Is it possible to perform an asynchronous cross-domain file-upload? .

Community
  • 1
  • 1
BrianFreud
  • 7,094
  • 6
  • 33
  • 50