Home > Web Front-end > JS Tutorial > jQuery+AJAX calls the background of the page

jQuery+AJAX calls the background of the page

php中世界最好的语言
Release: 2018-04-04 15:37:59
Original
1822 people have browsed it

This time I will bring you the background of jQuery+AJAX calling page, what are the precautions of the background of jQuery+AJAX calling page, the following is a practical case, let’s take a look .

The example in this article shares the jQuery AJAX calling page background method for your reference. The specific content is as follows

1. Create a new demo.aspx page.

2. First add reference to the background file demos.aspx.cs of the page.

using System.Web.Services;

1).Method call without parameters.
Please note that this version cannot be lower than .net framework 2.0 . It is not supported in version 2.0.
Backend code:

[WebMethod]   
public static string SayHello()   
{   
   return "Hello Ajax!";   
}
Copy after login

JS code:

$(function() {   
  $("#btnOK").click(function() {   
    $.ajax({   
      //要用post方式   
      type: "Post",   
      //方法所在页面和方法名   
      url: "Demo.aspx/SayHello",   
      contentType: "application/json; charset=utf-8",   
      dataType: "json",   
      success: function(data) {   
        //返回的数据用data.d获取内容   
        alert(data.d);   
      },   
      error: function(err) {   
        alert(err);   
      }   
    });   
  
    //禁用按钮的提交   
    return false;   
  });   
});
Copy after login

Page code:

  <form id="form1" runat="server">
  <p>
    <asp:Button ID="btnOK" runat="server" Text="验证用户" />
  </p>
  </form>
Copy after login

The running effect is as follows:

2). Method call with parameters
Backend code:

[WebMethod]   
public static string GetStr(string str, string str2)   
{   
  return str + str2;   
}
Copy after login

JS code:

$(function() {   
  $("#btnOK").click(function() {   
    $.ajax({   
      type: "Post",   
      url: "demo.aspx/GetStr",   
      //方法传参的写法一定要对,str为形参的名字,str2为第二个形参的名字   
      data: "{'str':'我是','str2':'XXX'}",   
      contentType: "application/json; charset=utf-8",   
      dataType: "json",   
      success: function(data) {   
        //返回的数据用data.d获取内容   
         alert(data.d);   
      },   
      error: function(err) {   
        alert(err);   
      }   
    });   
  
    //禁用按钮的提交   
    return false;   
  });   
});
Copy after login

The operation effect is as follows:

3).Return arrayMethod
Backend code:

[WebMethod]   
public static List<string> GetArray()   
{   
  List<string> li = new List<string>();   
  
  for (int i = 0; i < 10; i++)   
    li.Add(i + "");   
  
  return li;   
}
Copy after login

JS code:

$(function() {   
  $("#btnOK").click(function() {   
    $.ajax({   
      type: "Post",   
      url: "demo.aspx/GetArray",   
      contentType: "application/json; charset=utf-8",   
      dataType: "json",   
      success: function(data) {   
        //插入前先清空ul   
        $("#list").html("");   
  
        //递归获取数据   
        $(data.d).each(function() {   
          //插入结果到li里面   
          $("#list").append("<li>" + this + "</li>");   
        });   
  
        alert(data.d);   
      },   
      error: function(err) {   
        alert(err);   
      }   
    });   
  
    //禁用按钮的提交   
    return false;   
  });   
});
Copy after login

Page code:

<form id="form1" runat="server">
<p>
  <asp:Button ID="btnOK" runat="server" Text="验证用户" />
</p>
<ul id="list">
</ul>
</form>
Copy after login

Running result graph:

# 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:

Province, city and county three-level linkage when ajax does not refresh

After AJAX realizes the display page Just loading

The above is the detailed content of jQuery+AJAX calls the background of the page. 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