[英]Updated … need an Ajax example about how client side gets a value from the server side w/o postback
basically I want to check whether a user is logged in with javascript when a button is clicked 基本上我想检查单击按钮时用户是否使用javascript登录
I know ajax can do this, but I have no idea where to start... and I do not want to learn the whole ajax 我知道ajax可以做到这一点,但是我不知道从哪里开始...而且我也不想学习整个ajax
is there any simple code sample I can "borrow"? 有没有我可以“借用”的简单代码示例?
==============================UPDATE 2=================================== =============================更新2 ================= =================
I posted another question for this one and get the answer below... in case someone has the same question as me... 我为此发布了另一个问题,并在下面获得了答案...以防万一有人和我有相同的问题...
Can I return true to asp.net OnClientClick After an Ajax call? 我可以在Ajax调用后将true返回到asp.net OnClientClick吗? Or maybe some other way?
还是其他方式?
==============================UPDATE===================================== ==============================更新=================== ==================
I read a few examples but I just cannot find the exact one I need. 我读了一些例子,但是我找不到我需要的确切例子。 I can get the result from the server side and then... stuck... here is my code and please help with the questions in the comments.
我可以从服务器端获取结果,然后...卡住...这是我的代码,请在注释中提供帮助。 Thanks a lot!!!
非常感谢!!!
in .ascx: 在.ascx中:
<asp:Button ID="buttonSubmit" Text="Submit" OnClientClick="return buttonSubmitOnclick()" OnClick="buttonSubmit_Click" runat="server"/>
in .js: 在.js中:
function buttonSubmitOnclick() {
showLogin();
//What to do here??? how to return true if the ajax call return 'true'?
return true; //???
}
//========== Ajax ==============
function showLogin() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText == "false") {
$find('modalPopupLogin').show(); //Show Login Dialog
};
}
}
xmlhttp.open("GET", "AjaxService.aspx?t=auth", true); //??? should the last parameter be "false"???
xmlhttp.send();
}
This is pretty straight forward. 这很简单。 Since you are using asp.net probably the easiest way to do this is with a page method.
由于您正在使用asp.net,最简单的方法可能是使用分页方法。 Page Methods in asp.net allow you to write .NET code in your code behind and then call it using a javascript method.
asp.net中的Page Methods允许您在后面的代码中编写.NET代码,然后使用javascript方法进行调用。 The page method must be declared static/shared since it will be accessed outside of the page life cycle.
页面方法必须声明为静态/共享,因为它将在页面生命周期之外进行访问。
Here are some examples: 这里有些例子:
- http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx -http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
- http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ -http://encosia.com/using-jquery-to-direct-call-aspnet-ajax-page-methods/
Page Methods are pretty straight forward to call, with or without jQuery. 无论有无jQuery,页面方法都可以直接调用。
There is some good information on using Ajax without jQuery at w3 that has a working example of ajax with asp at the end. 关于在w3上使用不带jQuery的Ajax的一些很好的信息 ,最后有一个带asp的ajax的工作示例。 Not too much reading involved.
没有太多的阅读。
Updated for question changes: At the bottom explains the differences in using asynchronous true vs false . 已针对问题更改进行了更新: 在底部解释了使用异步true与false的区别 。
Using async=true, the JS isn't waiting around for the response so you don't want to handle it 'here': 使用async = true,JS不会在等待响应,因此您不想在“这里”处理它:
function buttonSubmitOnclick() {
showLogin();
//What to do here??? how to return true if the ajax call return 'true'?
return true; //???
}
Once the xmlhttp.send() is run, js moves on, returns out of showLogin() and will return out of buttomSubmitOnclick, regardless of if the login response has come back yet. 一旦运行xmlhttp.send(),js就会继续前进,从showLogin()中返回,并从buttomSubmitOnclick中返回,而不管登录响应是否回来了。
Assuming async true, as you have it, you'll need to expand the conditional in the onreadystatechange function to handle when the responseText == "true". 假设异步为true,那么您需要在onreadystatechange函数中扩展条件以在responseText ==“ true”时进行处理。
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText == "false") {
$find('modalPopupLogin').show(); //Show Login Dialog
}else if (xmlhttp.responseText == "true") {
//Continue the previous/submit action here because we are already authenticated
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.