简体   繁体   English

更新了…需要一个有关客户端如何从服务器端获取值而无需回发的Ajax示例

[英]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.

相关问题 如何将Ajax参数中的数组值从客户端传递到服务器端 - How To pass Array value in Ajax Parameter from Client side To Server side 如何从客户端到服务器端获取价值 - how to recive value from client side to server side AJAX:如何在单击按钮时在客户端和服务器端更改值? - AJAX: How to change a value on client side as well as server side on button click? 回发后如何从客户端保留值 - How to preserve the values from client-side after postback 如何使用 Ajax 从服务器端(nodejs)发送 JSON 到客户端? - How to send JSON from server-side (nodejs) to client-side with Ajax? 如何使用JQUERY使用AJAX将Javascript变量(从客户端)发布到PHP文件(服务器端) - How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY 如何以 GET 方法将数据从客户端发送到服务器端 - ajax - how to send data in GET method from client side to server side - ajax 如何在不使用处理程序的情况下使用ajax从客户端将文件发送到服务器端? - How to send file to server side from client side using ajax without using handler? 从客户端脚本获取值并覆盖服务器端脚本 - Get value from client side script and override server side script AppMaker从服务器端向客户端返回值 - AppMaker Return value from server side to client side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM