Home  >  Article  >  Web Front-end  >  How to implement ajax to call background defined methods based on JavaScript_jquery

How to implement ajax to call background defined methods based on JavaScript_jquery

WBOY
WBOYOriginal
2016-05-16 15:22:551341browse

Due to the unique advantages of ajax, it has been widely used in a large number of current websites. Here is an introduction to how ajax calls functions defined in the background. Although it is relatively simple, I hope it can bring some help to beginners. The code As follows:

1. First we create an antzone.aspx page.

2. Create the following function in its cs file:

public static string mytest(string first, string second) 
{ 
  return return first+second; 
} 

2.html code is as follows:

<form id="myform" runat="server"> 
<div> 
 <asp:Button ID="bt" runat="server" Text="点击测试" /> 
</div> 
</form> 

3.javascript code is as follows:

$(function(){ 
 $("#bt").click(function(){ 
  $.ajax({ 
   type:"Post", 
   url:"antzone.aspx/mytest", 
   data:"{'first':'蚂蚁部落','second':'欢迎您'}", 
   contentType:"application/json;charset=utf-8", 
   dataType:"json", 
   success:function(data){ 
    //返回的数据用data.d获取内容 
    alert(data.d); 
   }, 
   error:function(err){ 
    alert(err); 
   } 
  }); 
  //禁用按钮的提交 
  return false; 
 }); 
}); 

The above code implements the functions we need and can call functions defined in the background. The method of how to implement ajax call background definition based on jQuery will be introduced here. If you don’t understand anything, please leave a message. Thank you.

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