当我点击按钮时: - 迭代所有tr并将其所有输入的名称收集到一个数组中(已经完成) - 我还从两个文本中获取数据,这两个文本的名称是输入和触发 - 通过ajax将所有这些(一个数组和两个文本)发送到asp.net.cs(不工作)
var senders = [];
$("#btn_input_kaydet").click(function () {
var input = $("#dd_input").text();
var trigger = $("#dd_input_trigger").text();
$("#dynamic_input_field tr").each(function (i) {
senders[i] = $(this).find('td input').attr('name');
});
$.ajax({
type: "POST",
url: "senaryo.aspx/addscnerio",
data: { "arr": senders, "input": input, "trigger": trigger},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("successful")
},
traditional : true
});
});
[WebMethod]
public static void addscnerio(List<String> values, string name, string trigger)
{
}
答案 0 :(得分:1)
您将数据作为对象传递
data : { "arr": senders, "input": input, "trigger": trigger}
因此,您应该使用适当的类作为反映您传递的对象的参数,如下所示
public class InputData
{
public string name { get; set; }
public string trigger { get; set; }
public List<String> values { get; set; }
}
使用class作为参数
更改webmethod语法[WebMethod]
public static void addscnerio(InputData data)
答案 1 :(得分:0)
假设您的表格为var data = [ { "param": "type", "id": 6, "data": [{"name": "Collect", "filterKey": "collect", "checked": false }, {"name": "Redeem", "filterKey": "redeem", "checked": true }]}, {"param": "country","id": 5,"data": [{"name": "Malaysia","filterKey": "my","checked": true}, {"name": "Singapore","filterKey": "sg","checked": false}]}];
var result = [].concat(...data.map(o => o.data)).filter(d => d.checked).map(d => d.filterKey);
console.log(result);
,您可以执行以下操作
dynamic_input_field
然后在你的ajax中发送var senders= [];
var headers = [];
$('#dynamic_input_field th').each(function(index, item) {
headers[index] = $(item).html();
});
$("#dynamic_input_field tr").has('td').each(function() {
var arrayItem = {};
$('td', $(this)).each(function(index, item) {
arrayItem[headers[index]] = $(item).html();
});
senders.push(arrayItem);
});
答案 2 :(得分:0)
var subject= new Array();
subject[0] = ‘PHP’;
subject[1] = ‘ASP.net’;
$.ajax({
type: "POST",
url: "subject.aspx/GetSubject",
data: "{‘subject’:['PHP','ASP.net']}",
答案 3 :(得分:0)
我解决了这个问题。为了帮助其他人,我发布了工作代码:
var senders = [];
$("#btn_input_kaydet").click(function () {
var input = $("#dd_input").text();
var trigger = $("#dd_input_trigger").text();
$("#dynamic_input_field tr").each(function (i) {
senders[i] = $(this).find('td input').attr('name');
});
$.ajax({
type: "POST",
url: "senaryo.aspx/Save_Scenario",
data: '{arr: ' + JSON.stringify(senders) + ', input: "' + input + '", trigger: "' + trigger + '" }',
traditional: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("basarili");
},
error: function () {
alert("error");
}
});
});
[WebMethod]
public static void Save_Scenario(List<string> arr, string input, string trigger)
{
}
答案 4 :(得分:0)
据我所知你可以这样做。
代码1
在* .aspx文件或* .js文件中添加以下代码。
var senders = [];
$("#btn_input_kaydet").click(function () {
var input = $("#dd_input").text();
var trigger = $("#dd_input_trigger").text();
$("#dynamic_input_field tr").each(function (i) {
senders.push($(this).find('td input').attr('name'));
});
$.ajax({
type: "POST",
url: "yourpage.aspx/method",
data: '{array: ' + JSON.stringify(senders) + ', input: "' + input + '", trigger: "' + trigger + '" }',
traditional: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
senders = []; // Reset this so on Next Click you don't get same data
// TODO: Success Handler
},
error: function () {
senders = []; // Reset this so on Next Click you don't get same data
// TODO: Fail Handler
}
});
});
在服务器端代码文件中使用
[WebMethod]
public static void method(List<string> array, string input, string trigger)
{
// TODO: Further Action
}
在此Asp.Net中将JSON反序列化为自动字符串LIST。 Here, is good explanation for JSON Serialization and Deserialization in ASP.NET
代码2
当我不了解ASP.NET中的JSON序列化和反序列化时,我曾经这样做过
在* .aspx文件或* .js文件中添加以下代码。
var senders = "";
$("#btn_input_kaydet").click(function () {
var input = $("#dd_input").text();
var trigger = $("#dd_input_trigger").text();
$("#dynamic_input_field tr").each(function (i) {
senders += ","+$(this).find('td input').attr('name');
});
$.ajax({
type: "POST",
url: "yourpage.aspx/method",
data: '{array: ' + senders + ', input: "' + input + '", trigger: "' + trigger + '" }',
traditional: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// TODO: Success Handler
},
error: function () {
// TODO: Fail Handler
}
});
});
在服务器端代码文件中使用
[WebMethod]
public static void method(string strArray, string input, string trigger)
{
string[] array = strArray.Trim(",").Split(',');
// TODO: Further Action
}
通过这两种方式,您可以实现目标。