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

后端 未结 8 1621
孤城傲影
孤城傲影 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.

提交回复
热议问题