浏览器忽略来自ajax响应的标头刷新

In my javascript I have some ajax requests $.getJSON({...}) for various actions. I have a php app that handles these ajax requests. Before processing the actual request, my app first checks the session and in the case the user hasn't logged in yet, it sends a refresh signal back. Something like:

if (not logged in) {
header('Refresh: 0;');
}
else {
//process request
}

But the client doesn't actually refresh. Is there something I'm missing when it comes to AJAX requests and the http refresh header?

AJAX requests don't affect the browser until told to do so. Meaning, if i fetch a page using AJAX, it gets returned, maybe stored in a variable and that's it. It does not do anything after that. You need to parse the return data and act accordingly.

In your case, you might rather return something like JSON, have the client side parse what the return data meant, and act accordingly.

i tend to do something like:

{
    "directives": {
        //contains directives what to do first before parsing the data
        "whatToDo" : "redirect" 
    },
    "payload" : {
        //actual page data
    }
}

So the file your calling in your request contains the header('Refresh: 0;'); ? Because that has no effect at all, what i would do is if the user is not logged in return an unique string like so:

if (not logged in) {
    echo "NOTLOGGEDIN";
}
else {
    //process request
}

and then in your AJAX request check if they are logged in:

$.get("someURL", function(data) {
    if(data == "NOTLOGGEDIN"){
        //Do something
    } else{
        //Display data
    }
});