如何使用jquery将参数数组发送到asp.net web方法
使用下面的 jquery,我尝试将数组 paramlist 提交到下面适当的 Web 方法。我在这里缺少什么?
<script type="text/javascript">
$(document).ready(function () {
var slider = $('.slider').slider({
range: "min",
min: 0,
max: 100,
change: function (e, ui) {
var paramList = new Array();
var values = $('.slider').each(function () {
var s = $(this);
var aType = s.attr('itemName');
var point = s.slider("option", "value");
paramList.push(aType);
paramList.push(point);
});
CallPageMethod("SliderChanged", paramList, success, fail);
// $("#img1").fadeOut();
// alert("done");
},
slide: function (e, ui) {
var point = ui.value;
$("#selected_value").html(point);
// var width = 100 - point;
// $("#range").css({ "width": point + "%" });
}
});
function CallPageMethod(methodName, paramArray, onSuccess, onFail) {
//create list of parameters in the form
//{"paramName1":"paramValue1","paramName2":"paramValue2"}
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ",";
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
//get the current location
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
//call the page method
$.ajax({
type: "POST",
url: loc + "/" + methodName,
data: paramList,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
fail: onFail
});
}
function success(response) {
var lbl = $('#<%= Label1.ClientID %>')
lbl.html("Your report is now ready for download.");
alert(response.d);
}
function fail(response) {
alert("An error occurred.");
}
});
</script>
我有以下网络方法:
[WebMethod]
public static string SliderChanged(string[] values)
{
return "successArray";
}
using the jquery below I'm trying to submit the array paramlist to the appropriate web method below. What am I missing here?
<script type="text/javascript">
$(document).ready(function () {
var slider = $('.slider').slider({
range: "min",
min: 0,
max: 100,
change: function (e, ui) {
var paramList = new Array();
var values = $('.slider').each(function () {
var s = $(this);
var aType = s.attr('itemName');
var point = s.slider("option", "value");
paramList.push(aType);
paramList.push(point);
});
CallPageMethod("SliderChanged", paramList, success, fail);
// $("#img1").fadeOut();
// alert("done");
},
slide: function (e, ui) {
var point = ui.value;
$("#selected_value").html(point);
// var width = 100 - point;
// $("#range").css({ "width": point + "%" });
}
});
function CallPageMethod(methodName, paramArray, onSuccess, onFail) {
//create list of parameters in the form
//{"paramName1":"paramValue1","paramName2":"paramValue2"}
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ",";
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
//get the current location
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
//call the page method
$.ajax({
type: "POST",
url: loc + "/" + methodName,
data: paramList,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
fail: onFail
});
}
function success(response) {
var lbl = $('#<%= Label1.ClientID %>')
lbl.html("Your report is now ready for download.");
alert(response.d);
}
function fail(response) {
alert("An error occurred.");
}
});
</script>
I have the following web methods:
[WebMethod]
public static string SliderChanged(string[] values)
{
return "successArray";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
正如其他人所说,您需要指定参数名称;因此,
应该是:
As other people have said, you need to specify the parameter name; therefore,
Should be: