pass string array via jQuery AJAX to C# WebMethod pass string array via jQuery AJAX to C# WebMethod ajax ajax

pass string array via jQuery AJAX to C# WebMethod


Quick Fix

JSON arrays do not need to be in quotes. This is valid JSON:

{    "OriginalColorHex": [        "#000000",        "#006565",        "#cccc99"    ]}

Try validating your JSON with a tool like JSONLint to make sure it's valid. The WebMethod should be able to accept a string array just fine.

A slightly better method

Instead of building your JSON as a string, build an object and then let JavaScript handle the conversion for you:

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);    }});

Much cleaner, less error-prone, and easier to test. Here's a fiddle to demonstrate.


Because the values are not an array. Remove the quotes around the strings that look like an array.

{ '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' }


You are passing a string ('["#000000","#006565","#cccc99"]') into a string[]. Get rid of the single quotes around your array's. This should do it:

$.ajax({type: "POST",       //GET or POST or PUT or DELETE verb          url: PageURL + 'ChangeColor',       // Location of the servicedata:   "{ '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 serverdataType: "json",   //Expected data format from serverprocessdata: true,  //True or False      traditional: true,          success: function (result) {//On Successful service call    console.log(result);}

});