Trying to return this list as an array so I can then grab it on the client side via JS and iterate through its values.
So one of my .ashx methods returns a list like this in the end of the method:
equipmentTypes is a generic list of strings
_httpContext.Response.ContentType = "text";
_httpContext.Response.Write(equipmentTypes.ToArray());
When i receive it on the client, I get the literal text "System.String[]". So what content type should I return? Obviously this is not correct.
Then I assume I can eval this into an array (object) on the js side.
You should use JSON when sending data to javascript. You SHOULD NOT use eval as this opens you up to many security vulnerabilities (i.e. XSS).
See this article for more on C# and JSON, including turning a C# array into JSON.
You should serialize the objects that you wish to write to the response stream as JSON, the content-type of which is application/json. You could either use the built-in DataContract serialization or a third-party open source library such as the NewtonSoft JSON.Net library.
On the client you should use JSON.parse rather than eval.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With