Form that makes browser redirect when accessed by either a regular form submit or an Ajax request - is this possible?

后端 未结 8 1546
孤城傲影
孤城傲影 2021-01-06 08:22

I have a web page with a form. When the user submits the form, I want the server to make the browser redirect to a different page from the form action. Right now, I am doing

相关标签:
8条回答
  • 2021-01-06 08:53

    Did you try using the error function instead of complete?

    $.ajax({
        url: this.action,
        type: "POST",
        data: getFormData(this),
        error: function(request) {
            if(request.status == 302)
                window.location.assign(request.getResponseHeader("Location"));
        }
    });
    

    jQuery sends any 3xx response to the error function. Maybe the 'Location' header will still be available at this stage.

    0 讨论(0)
  • 2021-01-06 09:00

    Here is another option although you will need to do some cross browser tests:

    complete: function(request) {
        if(request.status == 200) {
            var doc = document.open(request.getResponseHeader('Content-Type'));
            doc.write(request.responseText);
            doc.close();
        }
    }
    

    Major drawbacks: URL in address bar doesn't change; could mess with back button/history

    Although I think @crescentfresh's idea is the way to go

    0 讨论(0)
  • 2021-01-06 09:06

    Pass additional parameter to your ajax request for easy identify type of request. When ajax - do not redirect - just send target url, then redirect client side in ajax callback via location.href

    like this:

    $.post('/controller/action', {formdata}, function (redirect_to) {
        location.href = redirect_to;
    });
    
    0 讨论(0)
  • 2021-01-06 09:09

    Will "complete" work:

    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        complete: complete(xhr, status) {
           window.location.assign(xhr.getResponseHeader("Location"));
    
        }
    });
    
    0 讨论(0)
  • 2021-01-06 09:12

    HTTP 302 response are consumed silently by XmlHttpRequest implementations (e.g. jQuery's ajax function). It's a feature.

    The way I've solved this in the past is to detect for XmlHttpRequests and issue a "Content-Location" header (rather than a "Location" header). The most cross-library way of doing this is to check for the "X-Requested-With" http header in your server-side code (jQuery, Prototype, Mootools among others set this):

    if (@$_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest') {
        header('Content-Location: ' . $redirect_url);
    } else {
        header('Location: ' . $redirect_url);
    }
    

    You still need to special-case your client-side code:

    $.ajax({
        // ...
        complete: function(xhr) {
            var redirect_url = xhr.getResponseHeader("Content-Location");
            if (redirect_url) {
                window.location = redirect_url;
            }
        }
    })
    
    0 讨论(0)
  • 2021-01-06 09:12

    Why not have your "ajax action" simply fill in the needed form fields and submit the form instead? This way you'll get exactly the same behavior as when submitting by hand.

    0 讨论(0)
提交回复
热议问题