1

Alright so I'm using Microsoft's Web Services and AJAX to get information from a SQL database for use with java script on the client side. And I'm wondering what the best method is. Before I started working on the project, the web services were setup to return a C# List filled with some objects. Those objects variables (ints, strings, etc.) contain the data I want to use. Of course, java script can't do much with this, to the best of my knowledge.

I then modified the web service to return a 2D Array, but java script got confused, and to the best of my knowledge can't handle 2D array's returned from C#. I then tried to use a regular array, but then a found the length property of an array in JS doesn't carry over, so I couldn't preform a for loop through all the items, because there wasn't anyway of knowing how many elements there were.

The only other thing I can thing of is returning a string with special char's to separate the data, but this seems way too convoluted. Any suggestions? Thanks in advance!

COB-CSU-AM
  • 61
  • 6

3 Answers3

1

EDIT

First, I'm assuming your using a SOAP based Webservice. By SOAP, more or less I'm talking about a webservice (in this case C#) that generates XML messages to accept requests for data and send back results from those requests. You can read more about SOAP here: http://en.wikipedia.org/wiki/SOAP

Going from a C# webservice to a C# client application is fairly simple since Visual Studio will automatically generate a consumer class for your client application. Example of this here: http://my.execpc.com/~gopalan/dotnet/webservices/webservice_csharp_client.html (though I think Visual Studio has better integration than this example shows).

More or less, your JavaScript will need to generate the appropriate SOAP request (XML) for your WebService to understand which method you want to invoke. After it has been invoked, you'll need to use JavaScript to parse the response (also XML) for the data you want (jQuery is great for this).

Here is a library that will help you make the request and get a response from your webserver. http://www.ibm.com/developerworks/webservices/library/ws-wsajax/

Here's a similar question on the same topic with a pretty good example : Simplest SOAP example

Let me know if this is what you're looking for. Hope it helps!

ORIGINAL

Your Data (2D Array, List, etc.) is being serialized to XML.

So you'll need your Javascript to parse the XML and not treat it like literal C# objects.

OR like keatch said, return JSON

Community
  • 1
  • 1
Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
  • I'm having trouble finding much information about using XML to do this, do you know of a good resource? Thanks! – COB-CSU-AM Jun 23 '11 at 12:51
0

Try to return a JSON array. JSON is a standard notation for passing javascript objects.

See this link for reference: http://www.codeproject.com/KB/aspnet/CSJSON.aspx

keatch
  • 2,187
  • 2
  • 17
  • 22
0

Try this in your service:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<SomeObject> name()
{
    ....
}
Joe
  • 80,724
  • 18
  • 127
  • 145