Skip to content Skip to sidebar Skip to footer

Pass C# Array To Javascript

What is the best way to pass C# array to javascript variable ? I have sample code but this return character by character from C# array, I want to return in normal way like word by

Solution 1:

Replace

var a = '<%= this.javaSerial.Serialize(this.names) %>';

with

var a = <%= this.javaSerial.Serialize(this.names) %>;

You were putting the resulting JSON into a javascript string, which would result in your example output iterating through each character of the Serialize call.

Solution 2:

your c# code will return a string, you have to first parse the string using JSON.parse, and then iterate through it:

var a = JSON.parse('<%= this.javaSerial.Serialize(this.names) %>');
for (var i = 0; i < a.length; i++) {
    console.log(a[i]);
}

or maybe as @Matthew said, don't put quotes around it, so you won't have to parse it.

Post a Comment for "Pass C# Array To Javascript"