我的代码存在一些问题。而且我想我会分享我的问题与这个网站的伟大头脑,看看有人可以给我的大脑一些指示如何解决它。JQuery,ajax逻辑问题
我正在开发一个应用程序(JQuery,PhoneGap),我有一个登录函数。登录功能将登录凭证发送到Web服务。所以我使用JQUery ajax发送它。 Web服务还包含一个fetchData函数,让我的客户端可以获取一些数据。 web服务将检查我是否正在登录,并且我的会话没有超时。如果它有超时我需要再次登录。然后再试一次。这是用户永远不会看到的东西。 loginUser()是一个在我的代码中多次使用的函数。所以我不想摆弄它,所以我不得不做很多改变。
因此,这里是一些伪代码:
function loginUser()
{
$.ajax(
{
....
success: function(data,status)
{
//everything worked great
return true;
},
error: function(data,status)
{
//display some user error stuff.
return false;
},
});
}
function getData()
{
$.ajax(
{
...
success: function(data,status)
{
//everything worked great. No need to login user again.
},
error: function(data,status)
{
//Opps user needs to login again and then we need to try again.
//If we have tried to getData after we tried to login (and got succeess from the loginUser() function)....report error.
//Below code will not work. But it will show you roughly what i am trying to do. It will also create an infinite loop.
if (loginUser())
{
getData();
}
},
});
}
和问题是什么? – Pluda
我该如何解决它?阅读代码评论,以便了解我需要做什么。 – Juw
我想这个问题可能会导致你的东西: http://stackoverflow.com/questions/6685249/jquery-performing-synchronous-ajax-requests。 –