如何从 jquery GET 搜索结果
我在调查脚本中 get
的结果时遇到了一些问题。我得到了以下代码来检查用户是否仍处于登录状态:
$.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";
}
});
现在,如果用户已登录,Struts 将从我的 get 中返回 result
中的一个 html 页面。如果用户已注销,将返回一个包含 id="loginInput"
元素的不同页面。
我以为上面的方法可以解决问题,但没有爱。我做错了什么?
有没有比使用随机 get
ping 服务器更好的方法?我需要一种使用 ajax 执行此检查的方法,并且注销时完成的任何 get
或 post
都会被拦截,结果将是登录页面而不是预期页面
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.
I thought the above would do the trick but no love. What am I doing wrong?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$('#result').get(......);
???更新
因此元素 #result 将具有 #loginInput 具有的内容
$('#result').load('login.php #loginInput');
否则,请查找 json 选项,因为您的方式这样做,有点乱
除非你正在寻找这个
$('#result').get(......);
???UPDATE
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
我找到了其他解决方案,在 Steven Benitez 的帮助下,我让它工作起来。请点击链接获取更多信息,但总之,我让我的拦截器来完成这项工作。如果调用某个路径/操作,拦截器将返回一个文本流,我可以从脚本中读取该文本流。
I found an other soloution 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.