通过 jQuery AJAX 将字符串数组传递给 C# WebMethod
Posted
技术标签:
【中文标题】通过 jQuery AJAX 将字符串数组传递给 C# WebMethod【英文标题】:pass string array via jQuery AJAX to C# WebMethod 【发布时间】:2015-06-28 19:11:23 【问题描述】:我想通过 jQuery (POST) 将 javascript 字符串数组传递给 C# WebMethod:
$.ajax(
type: "POST", // GET or POST or PUT or DELETE verb
url: PageURL + 'ChangeColor', // Location of the service
data: " 'OriginalColorHex': '" + JSON.stringify(clipartOriginalColorsHex) + "','ModifiedColorHex':'" + JSON.stringify(clipartModifiedColorsHex) +
"','OriginalColorRGB': '" + JSON.stringify(clipartOriginalColorsRGB) + "','ModifiedColorRGB':'" + JSON.stringify(clipartModifiedColorsRGB) +
"','fileName':'" + clipartFileName + "' ",
contentType: "application/json; charset=utf-8", // Content type sent to server
dataType: "json", // Expected data format from server
processdata: true, // True or False
traditional: true,
success: function (result) // On Successful service call
console.log(result);
);
ajax 调用中的数据如下所示
'OriginalColorHex': '["#000000","#006565","#cccc99"]', 'ModifiedColorHex': '["#3366CC","#cc5500","#3366cc"]', 'OriginalColorRGB': '["rgb(0,0,0)","rgb(0,101,101)","rgb(204,204,153)"]', 'ModifiedColorRGB': '["rgb(51, 102, 204)","rgb(204, 85, 0)","rgb(51, 102, 204)"]', 'fileName': '179.svg'
C#网络方法:
[WebMethod]
public static string ChangeClipartColor(string[] OriginalColorHex, string[] ModifiedColorHex, string[] OriginalColorRGB, string[] ModifiedColorRGB, string fileName)
// Code Here
错误
"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.String[]\u0027",
"StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
"ExceptionType":"System.InvalidOperationException"
【问题讨论】:
【参考方案1】:快速修复
JSON 数组不需要用引号引起来。这是有效的 JSON:
"OriginalColorHex": [
"#000000",
"#006565",
"#cccc99"
]
尝试使用JSONLint 之类的工具验证您的 JSON,以确保其有效。 WebMethod 应该能够接受字符串数组就好了。
稍微好一点的方法
与其将 JSON 构建为字符串,不如构建一个对象,然后让 JavaScript 为您处理转换:
var clipartOriginalColorsHex = ['#000000','#006565','#cccc99'];
var clipartModifiedColorsHex = ['#3366CC','#cc5500','#3366cc'];
var clipartOriginalColorsRGB = ['rgb(0,0,0)','rgb(0,101,101)','rgb(204,204,153)'];
var clipartModifiedColorsRGB = ['rgb(51, 102, 204)','rgb(204, 85, 0)','rgb(51, 102, 204)'];
var fileName = '179.svg';
var myData =
OriginalColorHex: clipartOriginalColorsHex,
ModifiedColorHex: clipartModifiedColorsHex,
OriginalColorRGB: clipartOriginalColorsRGB,
ModifiedColorRGB: clipartModifiedColorsRGB,
fileName: fileName
;
$.ajax(
type: "POST", //GET or POST or PUT or DELETE verb
url: PageURL + 'ChangeColor', // Location of the service
data: JSON.stringify(myData),
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true, //True or False
traditional: true,
success: function (result) //On Successful service call
console.log(result);
);
更简洁,更不容易出错,更容易测试。这是一个fiddle 来演示。
【讨论】:
【参考方案2】:因为值不是数组。删除看起来像数组的字符串周围的引号。
'OriginalColorHex': ["#000000","#006565","#cccc99"],'ModifiedColorHex':["#3366CC","#cc5500","#3366cc"],'OriginalColorRGB': ["rgb(0,0,0)","rgb(0,101,101)","rgb(204,204,153)"],'ModifiedColorRGB':["rgb(51, 102, 204)","rgb(204, 85, 0)","rgb(51, 102, 204)"],'fileName':'179.svg'
【讨论】:
【参考方案3】:您正在将字符串 ('["#000000","#006565","#cccc99"]') 传递给字符串[]。摆脱阵列周围的单引号。应该这样做:
$.ajax(
type: "POST", //GET or POST or PUT or DELETE verb
url: PageURL + 'ChangeColor', // Location of the service
data: " 'OriginalColorHex': " + JSON.stringify(clipartOriginalColorsHex) + ",'ModifiedColorHex':" + JSON.stringify(clipartModifiedColorsHex) +
",'OriginalColorRGB': " + JSON.stringify(clipartOriginalColorsRGB) + ",'ModifiedColorRGB':" + JSON.stringify(clipartModifiedColorsRGB) +
",'fileName':" + clipartFileName + " ",
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true, //True or False
traditional: true,
success: function (result) //On Successful service call
console.log(result);
);
【讨论】:
【参考方案4】:您可以等待将所有数据放在一起后对数据进行字符串化,从而让您的生活更轻松。
var data =
OriginalColorHex: clipartOriginalColorsHex,
ModifiedColorHex: clipartModifiedColorsHex,
OriginalColorRGB: clipartOriginalColorsRGB,
ModifiedColorRGB: clipartModifiedColorsRGB,
fileName: clipartFileName
;
$.ajax(
type: "POST", // GET or POST or PUT or DELETE verb
url: PageURL + 'ChangeColor', // Location of the service
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", // Expected data format from server
processdata: true, // True or False
traditional: true,
success: function (result) // On Successful service call
console.log(result);
);
【讨论】:
以上是关于通过 jQuery AJAX 将字符串数组传递给 C# WebMethod的主要内容,如果未能解决你的问题,请参考以下文章
使用 ajax 导入 JS 数组以传递给 JQuery 中的函数/插件
我可以通过 Ajax 将两个数组传递给控制器吗? (文件数组和整数数组)