读书人

asp.net 施用jQuery ajax 调用后台方法

发布时间: 2012-12-22 12:05:06 作者: rapoo

asp.net 使用jQuery ajax 调用后台方法..............................................新手在线求解


<a class="format">123</a><br />
<a class="format">456</a>



<script type="text/javascript">
$(".format").click(function(iFormat) {
$.ajax({
type: "post",
url: "test.aspx/chooseFormat",
data:{"str":iFormat},
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(‘’);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
</script>


[WebMethod]
public static void chooseFormat(string str)
{
string aaa = str;
}

问题描述:
1.我界面有好多的<a>,我想通过点击实现调用同页面后aspx.cs里的chooseFormat方法并传递参数 str
2.每个<a>在触发ajax时需要给iFormat不同的值,在<a class="format">123</a> 传递123 123的位置可以发生变化,比如:id="123"或其他
求解。求解.....
[最优解释]

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm15.aspx.cs" Inherits="Linq_Test.WebForm15" %>

<!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">
<title></title>
<script src="jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".format").each(function () {
$(this).bind("click", function () {


$.ajax({
type: "post",
url: "http://localhost:4321/test/WebForm15.aspx/GetStr", //项目是虚拟目录我就直接写成这了
data: "{'str':'" + $(this).text() + "'}",
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function (data) {
alert(data.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
});
});

});

</script>
</head>
<body>
<a href="#" class="format">content1</a> <a href="#" id="A1" class="format">content2</a>
<a href="#" id="A2" class="format">content3</a> <a href="#" id="A3" class="format">content4</a>
</body>
</html>
[code=csharp]

[WebMethod]
public static string GetStr(string str)
{
return str;
}


[/code]

上面代码经测试没有问题
[其他解释]
新建一个单独ashx文件专门处理,代码中获取传递的url值request["xxx"],如果页面只处理这个就不用写方法了直接运行方法中的代码即可。
传递不通知只需判断触发这个click事件的对象是哪个获取该对象的文本值作为传递的参数即可

[其他解释]

$(document).ready(function () {
$(".format").each(function () {


$(this).bind("click", function () {
$.ajax({
type: "post",
url: "test.aspx/chooseFormat",
data: { "str": $(this).text() },
datatype: "json",

contentType: "application/json; charset=utf-8",
success: function (data) {
alert("");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
});

});



试试
[其他解释]
引用:
XML/HTML code?123<a class="format">123</a><br /><a class="format">456</a>

JavaScript code?123456789101112131415161718<script type="text/javascript"> $(".format").click(function(……


第二个问题没看清,位置可以变化,到底怎么变化,怎么变化怎么取
[其他解释]

<a class="format" onclick="submit('123');">123</a><br />


<script type="text/javascript">
function submit(id){
$.ajax({
type: "post",
url: "test.aspx/chooseFormat?id="+id,
data:{"str":iFormat},
datatype: "json",
contentType: "application/json; charset=utf-8",


success: function(data) {
alert(‘’);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
</script>


[其他解释]
引用:
JavaScript code?



123456789101112131415161718192021

<a class="format" onclick="submit('123');">123</a><br /> <script type="text/javascript"> function submit(id){ $……



传递不同参数 +++
[其他解释]
2楼
提示错误
undefined

3楼
我的意思是不管123在<a>的什么位置 都没关系,只要js能够取到123就成

4楼
客户端调用服务器端后台方法

个人感觉2楼答案接近正解

[其他解释]
引用:
2楼
提示错误
undefined

3楼
我的意思是不管123在<a>的什么位置 都没关系,只要js能够取到123就成

4楼
客户端调用服务器端后台方法

个人感觉2楼答案接近正解


我的意思是不管123在<a>的什么位置 都没关系,只要js能够取到123就成
----------------------
在哪都可以取到

注意ajax路径,和data格式

读书人网 >asp.net

热点推荐