Home > Web Front-end > JS Tutorial > jQuery implements asynchronous refresh

jQuery implements asynchronous refresh

php中世界最好的语言
Release: 2018-04-25 14:23:40
Original
1891 people have browsed it

This time I will bring you jQuery to implement asynchronous refresh. What are the precautions for jQuery to implement asynchronous refresh? The following is a practical case, let's take a look.

Recently I have used jquery to read data asynchronously. jquery provides many built-in asynchronous reading functions. I will show you the most commonly used $.ajax usage

On the client Enter a content in the text box, and then return the time on the server side

The ashx file is used in DEMO to obtain server information

Effect pictures

The

escape() function encodes a

string so that it can be read on all computers.

Client-side code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<mce:script type="text/javascript" src="js/jquery-1.4.2.min.js" mce_src="js/jquery-1.4.2.min.js"></mce:script> 
 <title></title> 
 <mce:script type="text/javascript"><!-- 
  function GetData() { 
   if ($(&#39;#Text1&#39;).val() == &#39;&#39;) { 
    alert(&#39;请输入内容!&#39;); 
    return; 
   } 
   $.ajax({ 
    type: "GET", 
    url: "ContentHandler.ashx?name=" + $(&#39;#Text1&#39;).val(), 
    cache: false, 
    data: { sex: "男" }, 
    success: function(output) { 
     if (output == "" || output == undefined) { 
      alert(&#39;返回值为空!&#39;); 
     } 
     else { 
      output = eval("(" + output + ")"); 
      $(&#39;#pmsg&#39;).html("姓名:" + output.name + "----" + "日期:" + output.dt); 
     } 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     alert("获取数据异常"); 
    } 
   }); 
  } 
// --></mce:script> 
</head> 
<body> 
 <form id="form1" runat="server"> 
 <p> 
  ajax使用demo 
 </p> 
 <p> 
<input id="Text1" 
   type="text" /> 
     <input id="Button1" type="button" value="获取数据" onclick="GetData()"/> 
   </p> 
  <p id=&#39;pmsg&#39;> 
  </p> 
 </form> 
</body> 
</html>
Copy after login
Server-side code

<%@ WebHandler Language="C#" Class="ContentHandler" %> 
using System; 
using System.Web; 
public class ContentHandler : IHttpHandler { 
 public void ProcessRequest (HttpContext context) { 
  string output = ""; 
  string name = context.Request.Params["name"]; 
  output = GetJsonData(name); 
  context.Response.ContentType = "text/plain"; 
  context.Response.Write(output); 
 } 
 public bool IsReusable { 
  get { 
   return false; 
  } 
 } 
 public string GetJsonData(string aa) 
 { 
  string result = "{name:/""+aa+"/",dt:/""+DateTime.Now.ToString()+"/"}"; 
  return result; 
 } 
}
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

What are the methods for jQuery to terminate ajax requests

Detailed explanation of the steps of asp processing json data

The above is the detailed content of jQuery implements asynchronous refresh. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template