ajax的源碼
❶ 尋找與伺服器同步或非同步的Ajax源碼
function getuserinfo(userid){
$.ajax({
type: "get",//使用get方法訪問後台
dataType: "json",//返回json格式的數據
url: "/user/getuserinfobyid.php",//要訪問的後台地址
cache:false,
data:{userid:userid},//要發送的數據
success: function(msg){
alert(msg);
}
}
});
}
這個就是個簡單的例子 調用這個getuserinfo(userid)方法的時候把userid的值傳給了getuserinfobyid.php這個頁面 getuserinfobyid的到userid後就從資料庫里查詢到用戶id為userid的用戶信息然後以某種格式 數組或者json 返回,然後就返回的數據經過這個success:function(msg)函數處理 msg就是返回的數據 這里就是簡單的alert一下,一般都是要經過處理顯示的
❷ java後台獲取網頁ajax數據和返回數據簡單源碼
1新建一個servlet xml中相應配置(一般自動)
2創建service方法
3接受參數,做操作,返回數據
比如頁面發送ajax請求到SomeServlet
$.post("SomeServlet的請求路徑",{param:"param"},function(data){
//data為返回的數據以json形式
alert(data.id+""+data.name+""+data.age);
},"json");
Servlet
publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
Stringparam=request.getParameter("param");//獲取參數
//你的操作
//返回數據
Stringjson="{"id":1,"name":"張三","age":18}";
out.print(json);
}
❸ ajax asp.net智能匹配完成搜索完整源碼
ASP.net+AJAX智能匹配檢索(自動完成)
實例說明
使用谷歌搜索引擎,用戶只要輸入部分關鍵字,就能夠顯示相關搜索提示信息列表。那麼在這里我們通過ASP.NET來實現這樣的功能,程序運行結果如圖18.2所示。
圖18.2 智能匹配檢索
技術要點
本實例的核心技術是通過ASP.NET AJAX Control Toolkit中的AutoCompleteExtender控制項實現。
AutoCompleteExtender控制項實現自動輸入建議的功能,通過調用WebService或本頁面對應的方法名來獲取提示數據,供用戶能達到自動選擇的功能。AutoCompleteExtender控制項的主要屬性及說明如表18.2所示。
表18.2 AutoCompleteExtender控制項的主要屬性及說明
屬性
說明
TargetControlID
指定將被輔助完成自動輸入的控制項ID,這里的控制項只能是TextBox
ServicePath
指出提供服務的WEB服務路徑,若不指出則ServiceMethod表示本頁面對應的方法名
ServiceMethod
指出提供服務的方法名,例如public string[] Method(string prefixText, int count),其中參數prefixText是用戶輸入的關鍵字;參數count是所需要獲取提示數據的數量;兩個參數都會自動傳給WebService的ServiceMethod方法),返回值是用戶所獲得提示數據的來源數組。
MinimumPrefixLength
指出開始提供提示服務時,TextBox控制項應有的最小字元數,默認值為3
CompletionInterval
從伺服器讀取數據的時間間隔,默認為1000,單位:毫秒。
EnableCaching
是否在客戶端緩存數據,默認為true
CompletionSetCount
顯示的條數,默認值為10
實現過程
(1)新建一個AJAX網站,將其命名為Ex08_02,默認主頁為Default.aspx。
(2)在Default.aspx頁中主要添加一個ScriptManager控制項、一個AutoCompleteExtender控制項和一個TextBox控制項,其中ScriptManager控制項主要用於管理Web頁面中的AJAX控制項,AutoCompleteExtender控制項實現自動完成功能,TextBox控制項接收輸入檢索關鍵字。
(3)創建一個Web服務,將其命名為KeyFind.asmx,該服務主要完成智能檢索功能。
(4)在KeyFind.asmx Web服務的KeyFind.cs文件下實現代碼如下:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
//引入空間
using System.Data;
using System.Data.OleDb;
using System.Configuration;
/// <summary>
/// KeyFind 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//添加服務腳本(必須添,否則程序不能正常運行)
[System.Web.Script.Services.ScriptService]
public class KeyFind : System.Web.Services.WebService
{
public KeyFind()
{
//如果使用設計的組件,請取消注釋以下行
//InitializeComponent();
}
//定義數組保存獲取的內容
private string[] autoCompleteWordList = null;
//兩個參數「prefixText」表示用戶輸入的前綴,count表示返回的個數
[WebMethod]
public String[] GetCompleteDepart(string prefixText, int count)
{
///檢測參數是否為空
if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;
// 如果數組為空
if (autoCompleteWordList == null)
{
//讀取資料庫的內容
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Ex18_02.mdb"));
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select keyName from keyInfo where keyName like'" + prefixText + "%' order by keyName", conn);
DataSet ds = new DataSet();
da.Fill(ds);
//讀取內容文件的數據到臨時數組
string[] temp = new string[ds.Tables[0].Rows.Count];
int i = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
temp[i] = dr["keyName"].ToString();
i++;
}
Array.Sort(temp, new CaseInsensitiveComparer());
//將臨時數組的內容賦給返回數組
autoCompleteWordList = temp;
if (conn.State == ConnectionState.Open)
conn.Close();
}
//定位二叉樹搜索的起點
int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
if (index < 0)
{ //修正起點
index = ~index;
}
//搜索符合條件的數據
int matchCount = 0;
for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
{ ///查看開頭字元串相同的項
if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
{
break;
}
}
//處理搜索結果
string[] matchResultList = new string[matchCount];
if (matchCount > 0)
{ //復制搜索結果
Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);
}
return matchResultList;
}
}
(5)回到Default.aspx頁的源視圖,設置其AutoCompleteExtender控制項屬性值,代碼如下:
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"
ServicePath="KeyFind.asmx" CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetCompleteDepart">
</cc1:AutoCompleteExtender>