Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AjaxSubmit with window.location on response

I have a problem with ajaxSubmit... The problem is that, when I submit a form and get a response from PHP that have a script with window.location, the page doesn't change at all...

My PHP script just returns a script with window.location when the form is submitted correctly, otherwise, it returns a JSON that is parsed on the success function.

Using the tools of Chrome, I see that it loads the window.location URL on the Network Tab but, it doesn't show it to the user...

Here's the code I have:

$("form").submit(function(e) {
    e.preventDefault();
    $("form").ajaxSubmit({
        success: function(resp){
            try{
                resp=JSON.parse(resp);
                alert(resp["error"]);
            } catch(e){
                $("<div></div>").html(resp);
            }

        }
    });
    return false;
});

How do I need to do to make it work? Is there any property of options parameter of AjaxSubmit that could solve that?

like image 850
Cristiano Santos Avatar asked Nov 03 '22 13:11

Cristiano Santos


1 Answers

Just provide URL without JS code in your server answer and then execute :

$("form").submit(function(e) {
    e.preventDefault();
    $("form").ajaxSubmit({
        success: function(resp){
            try{
                resp=JSON.parse(resp);
                window.location.href = resp;
            } catch(e){
                $("<div></div>").html(resp);
            }

        }
    });
    return false;
});
like image 123
sdespont Avatar answered Nov 09 '22 04:11

sdespont