Ich möchte ein JavaScript-String-Array über jQuery (POST) an eine C # WebMethod übergeben:
$.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);
}
});
Daten, die in einen Ajax-Anruf gehen, sehen so aus
{ '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:
[WebMethod]
public static string ChangeClipartColor(string[] OriginalColorHex, string[] ModifiedColorHex, string[] OriginalColorRGB, string[] ModifiedColorRGB, string fileName)
{
// Code Here
}
Error
{
"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"
}
Schnelle Lösung
JSON-Arrays müssen nicht in Anführungszeichen gesetzt werden. Dies ist gültige JSON:
{
"OriginalColorHex": [
"#000000",
"#006565",
"#cccc99"
]
}
Versuchen Sie, Ihr JSON mit einem Tool wie JSONLint zu überprüfen, um sicherzustellen, dass es gültig ist. Die WebMethod sollte in der Lage sein, ein String-Array zu akzeptieren.
Eine etwas bessere Methode
Anstatt Ihren JSON als String zu erstellen, erstellen Sie ein Objekt und lassen Sie JavaScript die Konvertierung für Sie übernehmen:
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);
}
});
Viel sauberer, weniger fehleranfällig und einfacher zu testen. Hier ist eine Geige zu demonstrieren.
Weil die Werte kein Array sind. Entfernen Sie die Anführungszeichen um die Zeichenfolgen, die wie ein Array aussehen.
{ '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' }
Sie übergeben einen String ('["# 000000", "# 006565", "# cccc99"]') an einen String []. Entfernen Sie die einfachen Anführungszeichen um Ihre Arrays. Das sollte es tun:
$.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);
}
});
Sie können Ihr Leben einfacher machen, indem Sie darauf warten, Ihre Daten zu stringifizieren, nachdem Sie alles zusammengefügt haben.
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);
}
});