Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net json web服务发送数组错误无法转换类型为';System.String';输入';System.Collections.Generic.List`1[System.String]';_C#_Jquery_Asp.net_Ajax - Fatal编程技术网

C# asp.net json web服务发送数组错误无法转换类型为';System.String';输入';System.Collections.Generic.List`1[System.String]';

C# asp.net json web服务发送数组错误无法转换类型为';System.String';输入';System.Collections.Generic.List`1[System.String]';,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我想使用ajax和webservice将数据从JavaScript插入数据库, 我有一些控件,通过jQuery我可以获取它们的值并生成它们的值的数组 发送阵列时,会导致以下错误: 无法将“System.String”类型的对象转换为“System.Collections.Generic.List1[System.String]”类型 这是C代码 [WebMethod] 公共对象插入(列表a) { 返回a; } 您需要使$.ajax调用的数据参数如下:JSON.stringify({'a':var

我想使用ajax和webservice将数据从JavaScript插入数据库, 我有一些控件,通过jQuery我可以获取它们的值并生成它们的值的数组

发送阵列时,会导致以下错误:

无法将“System.String”类型的对象转换为“System.Collections.Generic.List1[System.String]”类型

这是C代码

[WebMethod]
公共对象插入(列表a)
{
返回a;
}

您需要使
$.ajax
调用的
数据
参数如下:
JSON.stringify({'a':variables})

JSON
变量在JSON实现,如答案中提到的实现

另外,在
success
函数中还有一个额外的
}

因此,在将来,如果您想向传递给web服务的数据对象添加额外的参数,您可以这样构造它:

var someArr = ['el1', 'el2'];

JSON.stringify({ 
  'param1': 1,
  'param2': 'two'
  'param3': someArr
  // etc
});
JavaScript:

var variables = Array();

var i = 0;
$('#Div_AdSubmition').find('.selectBox').each(function () {

  variables[i] = $(this).find('.selected').find(".text").html();

  i++;
});



$.ajax({
    type: 'post',
    data: JSON.stringify({'a': variables}),
    dataType: 'json',
    url: 'HomeWebService.asmx/insertAd',
    contentType: 'application/json; charset=utf-8',
    success: function (data) {

      alert("data : " + data.d);
    });

});
C#

[WebMethod]
公共对象插入(列表a)
{
返回a;
}
数据:要发送到服务器的数据。如果尚未转换为字符串,则会将其转换为查询字符串。它被附加到GET请求的url。请参阅processData选项以防止此自动处理。对象必须是键/值对。如果value是一个数组,jQuery将基于传统设置的值用相同的键序列化多个值

因此,尝试向ajax调用添加一个新参数:

$.ajax({
    type: 'post',
    data: { 'a': variables }, // Notice some changes here
    dataType: 'json',
    traditional: true, // Your new parameter
    url: 'HomeWebService.asmx/insertAd',
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        alert("data : " + data.d);
    }
});
这样试试。也许您需要更改Web服务方法以期望使用
数组而不是
列表,但我不确定。

试试这个

            data: JSON.stringify({ 'a': variables }),

data:“{'a':'“+variables+“}”
发送一个包含
Object
或类似内容的字符串。@MelanciaUK但它们都是西班牙语的文本。我的错。我正在发布一个答案。
[WebMethod]
public object insertAd(List<string> a)
{
  return a;
}
$.ajax({
    type: 'post',
    data: { 'a': variables }, // Notice some changes here
    dataType: 'json',
    traditional: true, // Your new parameter
    url: 'HomeWebService.asmx/insertAd',
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        alert("data : " + data.d);
    }
});
            data: JSON.stringify({ 'a': variables }),