利用JQuery直接调用asp.net后台的简单方法

2020-05-27 18:04:08易采站长站整理

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;
});
});
///
<reference path="jquery-1.4.2-vsdoc.js"/>
$(function()
{
$("#btnOK").click(function()
{
$.ajax({
type:
"Post",
url:
"data.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;
});
});

4、返回Hashtable方法的调用

后台<C#>:


using System.Web.Script.Services;
using System.Collections;

[WebMethod]public static Hashtable
GetHash(string key,string value)
{
Hashtable
hs = new Hashtable();

hs.Add("www",
"yahooooooo");
hs.Add(key,
value);

return hs;
}

前台<JQuery>:


$(function()
{
$("#btnOK").click(function()
{
$.ajax({
type:
"Post",
url:
"data.aspx/GetHash",
//记得加双引号
T_T
data:
"{
'key': 'haha', 'value': '哈哈!' }",
contentType:
"application/json;
charset=utf-8",
dataType:
"json",
success:
function(data)
{
alert("key:
haha ==> "+data.d["haha"]+"n
key: www ==> "+data.d["www"]);
},
error:
function(err)
{
alert(err
+ "err");
}
});

//禁用按钮的提交
return false;
});
});

5、操作xml


XMLtest.xml:

view
plaincopy to clipboardprint?
<?xml version="1.0"
encoding="utf-8" ?>
<data>
<item>
<id>1</id>
<name>qwe</name>
</item>
<item>
<id>2</id>
<name>asd</name>