2012-06-08 29 views
0

我在我的ASP.NET MVC3网站中有一个部分,用户可以点击一个按钮在其账户中添加一个条目到他们的'保存项目'部分。这是通过JQuery Ajax请求完成的,如果它们已经登录,该请求会很好。如果它们没有登录,我希望它们被重定向到登录页面,然后自动将条目添加到它们的Saved项目部分。通过登录屏幕向JQuery发送数据

我有所有的部分单独工作 - 即当按钮被点击,如果没有登录,登录框显示。登录弹出窗口也可以成功运行。问题是试图一次完成所有事情。这里是我的代码至今:

的保存按钮Click事件 - 检查是否用户一路上登录:

var loggedIn = false; 

    $(document).ready(function() { 
     $('a#saveSearch').live('click', function (event) { 
      $.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) { 
       if (response == "True") 
        loggedIn = true; 
       else 
        loggedIn = false; 
      }); 
      if (loggedIn){ 
       SaveSearch(); 
      } 
      else{       
       $('#dialog').dialog('open'); 
       SaveSearch(); //don't think this is correct because it hits this line before login is complete 
      } 
     }); 

功能保存到数据库:

 function SaveSearch(){      
      var url = '@Url.Action("SaveSearch", "User")'; 
      $.ajax({ 
       url: url, 
       type: 'POST', 
       contentType: "application/json; charset=utf-8", 
       data: JSON.stringify({ 
        json: "@Html.Raw(Session["MyFormString"].ToString())" 
       }), 
       success: function (data) { 
        $('a#saveSearch').attr('disabled', "disabled"); 
        $('div#savedResponse').html('<p>Search saved to user account</p>'); 
       }, 
       error: function() { 
       } 
      }); 
     } 
    }); 

JQuery用户界面对话框弹出:

$(function() { 
    $('#dialog').dialog({ 
     autoOpen: false, 
     width: 400, 
     resizable: false, 
     title: 'Login', 
     modal: true, 
     open: function(event, ui) { 
      $(this).load("@Url.Action("Logon", "Account", null)"); 
     }, 
     buttons: { 
      "Close": function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 

我认为有一些根本性的错误与我的代码,是通过这种方式,登录弹出窗口只出现一秒钟,然后立即消失。它看起来像我需要让它停止前进的代码,直到登录已完成。

任何意见或帮助得到这个去将不胜感激。

+0

在对话框关闭后使用close方法放置什么。对话框“异步”完成。 –

回答

1

我可以想象您的问题可能涉及到:

$.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) { 
    if (response == "True") 
    loggedIn = true; 
    else 
    loggedIn = false; 
}); 
if (loggedIn){ 
    SaveSearch(); 
} 
else{       
    $('#dialog').dialog('open'); 
    SaveSearch(); //don't think this is correct because it hits this line before login is complete 
} 

$.get调用是异步的,这意味着后者代码:

if (loggedIn){ 

服务器响应之前被执行。你需要把你的回应回调中的代码:

$.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) { 
    if (response == "True") 
    loggedIn = true; 
    else 
    loggedIn = false; 

    if (loggedIn){ 
    SaveSearch(); 
    } 
    else{       
    $('#dialog').dialog('open'); 
    SaveSearch(); //don't think this is correct because it hits this line before login is complete 
    } 
}); 
+0

在评论中声明的对话框与e-on一样。 SaveSearch应该位于对话框工作流程 –

+0

是的,这是它 - 这是它排序感谢 –

1

尝试,并添加了密切的回调函数,以你的模式,那么代码只会尽快模式是封闭进行,所有登录已经完成成功地。在您的代码中查看注释

$(document).ready(function() { 
    $('a#saveSearch').live('click', function (event) { 
     $.get('@Url.Action("IsLoggedIn", "Account", null)', function (response) { 
      if (response == "True") 
       loggedIn = true; 
      else 
       loggedIn = false; 
     }); 
     if (loggedIn){ 
      SaveSearch(); 
     } 
     else{ 
      //in this dialog, add a close handler,then add the SaveSearch(); function in that handler 
      $('#dialog').dialog('open'); 

      } 
    });