2013-08-21 119 views
0

我不知道为什么下面的代码不起作用。 我使用jQuery 1.7.1:

<script src="~/Scripts/jquery-1.7.1.min.js"></script> 
我_layout

它显示alert('hello');,但是当我使用萤火它接着说:

RedirectToAction("MainIndex", "Home"); 

我觉得allthings是真实的。

<script type="text/javascript"> 

$(document).ready(function() { 

    $('#btn_submit').click(function() { 
     alert('hello'); 

     $.ajax({ 

      url: 'Product/IsUserPeresent', 
      cache: false, 
      type: 'Get', 
      success: function (result) { 

       alert(result); 
       @* var url = '@Url.Action("ncheckout", "Home")'; 
       $.post(url, { 
        name: result 
          });*@ 

      }, 
      error: function() { 
       RedirectToAction("MainIndex", "Home"); 
      } 
     }); 
    }); 
}); 

</script> 

public ActionResult IsUserPeresent(){ 

    //var nam = User.Identity.Name; 

    var nam = "alex"; 
    return Json(nam, JsonRequestBehavior.AllowGet); 
} 
+0

看萤火虫的Net标签。在那里你会看到AJAX请求,并可以检查服务器返回的响应。它是什么? 404? 500?也可以使用Url助手而不是将URL硬编码到控制器操作:'url:'@ Url.Action(“IsUserPeresent”,“Product”)''。 –

+2

它看起来像你有一个剃刀评论... –

回答

1

试试这个:

$('#btn_submit').click(function (event) { 
    event.preventDefault(); 

    alert('hello'); 

    $.ajax({ 

     url: 'Product/IsUserPeresent', 
     cache: false, 
     type: 'Get', 
     success: function (result) { 

      alert(result); 
      @* var url = '@Url.Action("ncheckout", "Home")'; 
      $.post(url, { 
       name: result 
         });*@ 

     }, 
     error: function() { 
      window.location = '@Url.Action("MainIndex", "Home")'; 
     } 
    }); 
});