
Question:
I'm having a bit of a problem investigating the result from my get
in my script. I got the following code to check if a user is still logged in:
$.get("nowhereGet", function(result){
if($(result).find('[id="loginInput"]'))
{
//HTML is in the response then we have been logged out and the user needs to go to
window.location.href = "login";
}
});
Now if the user is logged in Struts will return one html-page in result
from my get. If the user has been logged out different page will be returned one with and element id="loginInput"
in it.<br />
I thought the above would do the trick but no love. What am I doing wrong?<br />
Is there a better way to do this than to ping the server with a random get
? I need a method that performes this check using ajax and any get
or post
done while logged out will get intercepted and the result will be the login-page instead of the intended page
$('#result').get(......);
???
<strong>UPDATE</strong> so the element #result would have what ever the #loginInput has
$('#result').load('login.php #loginInput');
Otherwise look for a json option because the way you are doing it, its kinda messy
Unless you are looking for this
$.get("nowhereGet", function(result){
if(result && $(result).find('#loginInput').length)
{
//HTML is in the response then we have been logged out and the user needs to go to
window.location.href = "login";
}
});
Answer2:I found an <a href="https://stackoverflow.com/questions/6042843/struts-interceptor-giving-a-stream-result" rel="nofollow">other soloution</a> and with help from Steven Benitez I got it working. Follow the link for more information but in short I let my interceptor do the work instead. If a certain path/action is called the interceptor will return a text stream that I can read from my script.