Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax and redirect response from server

I have a situation where I send jquery ajax post request but in my web application, the handler for this specific ajax request (after processing post parameters), will invoke an action in another controller ( not sure if this is called redirect) which basically renders the whole website page ( like page refresh). But I notice browser keeps display the same page instead of refreshing to the content of the new page.

Is there something wrong? How can I deal with this situation?

I had to edit my question because I changed my ajax call.

This is what the code looks like:

function chkSubmit(event, actionType) {


        var msgid = showlst('Please wait ...');
        var data = ''
        if (actionType == 'IAmDone') {
            var letters = 'e,b,c'

            data = 'actionType=' + actionType + '&letters=' + letters;

        } else data = 'actionType=' + actionType;
        $j.ajax({
            type: 'POST',       
            url: context + '/app/handleChk',
            data:  data
        });

        return false;
}

The above function runs when a button on the page is clicked. But this same page keep displaying. The browser debugger shows that it did receive 200 OK response from the new action which it was supposed to refresh page with. I am using Chrome browser and jquery 1.6.1

Sorry for my typo in code sample. I corrected it.

like image 923
rjc Avatar asked Jul 07 '11 13:07

rjc


2 Answers

The server can not do a redirect from an ajax request. In the end ajax involves the client (browser). If you want to redirect you can do it, but it is going to have to be done on client side, in the callback. You can do that by returning an object from the server which contains the url you want to redirect to--- you can then you javascript to change the document's location property. I would think that this would make sense if you were not redirecting in all cases, or if your server side call was a long running process. If neither is the case then an ajax call probably doesn't make sense in the first place.

like image 89
ek_ny Avatar answered Oct 08 '22 18:10

ek_ny


I may be misreading your question, but where is your success callback function in that ajax call? That is where you would typically render the results into the view, also, you could use the error callback to get some data on what, if anything is going wrong:

function chkSubmit(event, actionType) {


        var msgid = showlst('Please wait ...');
        var actionType = type // per j. tuskan - looks like no such var in scope
        var data = ''
        if (actionType == 'IAmDone') {
            var letters = 'e,b,c'

            data = 'actionType=' + actionType + '&letters=' + letters;

        } else data = 'actionType=' + actionType;
        $j.ajax({
            type: 'POST',       
            url: context + '/app/handleChk',
            data:  data,
            success:function(the_data){
              alert("Now I can do stuff with the ajax response which is: "+the_data);
            }
        });

        return false;
}
like image 32
picus Avatar answered Oct 08 '22 19:10

picus