Sending information between JavaScript and Web Services using AJAX - c#

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!

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

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

Try this in your service:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<SomeObject> name()
{
....
}

Related

Using custom class methods in web service client (C#)

I've been doing quite a bit a learning over the past couple of days and I believe I've come to a conclusion. I would like to ask if my current understanding is correct, as I've had a very difficult time getting a "straight" answer to my specific problem based on Google searches.
I have a C# class library (.dll) with some classes that are used across multiple projects, including customClass (namespace myDLL).
I have a C# web service. One of the web service functions has an "out customClass [] myCustomClass" array argument.
I have a C# web service client. I want to be able to use customClass in the client. Here is a list of questions:
1) Is it true that the customClass array that is returned to the web service client is NOT the same type as an array of type myDLL.customClass (even though it is intuitive to assume that it would be)?
2) Is there any easy way to cast/convert the customClass array in the web service client to the type myDLL.customClass? I'd prefer to not have to write supporting code or manually edit reference.cs. It is my belief that there is NOT such an easy method of conversion, although it can be done with supporting conversion code.
3) It is my belief that the output of web services is meant to be only data (in general). To expect that a client will get class behavior along with data "easily/natively" when using the web service WSDL is not an expectation supported by the web service/web service client model. Is this belief generally correct?
I appreciate any help. If anyone has an nice, straightforward links about this topic, I wouldn't be surprised I've already read it, but I will definitely be all eyes :).
Thanks,
Richard

NSJSONSerialization in iOS 5 like c# serialize to class

I am porting our C# MVC Repository code to iOS5 for the iPad. I have been working successfully with the calls to the services, pulling Json and serializing to built in NS objects. But this seems like a lot of work to pull the pieces out and then assign them to a class. In C# this is a breeze, just serialize to your class, mapped to the data attributes and you are off with a strong typed view model.
Anyone done something similar in iOS/XCode and I know you can use NSData in interesting ways, I am just not expert enough yet and I am looking for pointers and best practces.
You question is not very clear to me. But as per my interpretation, you want to consume web service data in your application.In that case, see if it helps to you:
Are you able to connect to web service and get serialised data from it? You will receive data in object of type NSData. Then you you can use NSXMLParser class (initialised with NSData received from web service) and it's delegate methods to parse the data.
This blog may help you:
http://iphonebyradix.blogspot.com/2011/04/working-with-webservices.html
If you want to write serialised data, using NSJSONSerialization class then you can use
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error
method. You can find details on developer.apple :
https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
Try the KeyValueObjectMapping project on github, it has some good support for object mapping(auto) and remapping if the property/class is named different from the context of the json.

Custom Business Object : AJAX Enabled WCF

Can I pass a custom object between AJAX enabled WCF and my asp.net page?
I searched the web but could not find any examples. Most shows simple types like string and integers.
I also do not know how to populate custom object's property through JavaScript on the client side.
We have a browser add on and we have to pass data to that addon from a web service, I researched and looks like AJAX enabled WCF is way to go
Using .net framework 3.5 and VS 2008
You can't pass the actual custom objects, but you can of course pass the serialized version of them through your service and to your page, javascript, etc. Basically, you have to map the fields of your complex custom .NET types to classes decorated with the DataContract attribute. These classes are the types that your service will return. The DataContract-decorated classes will contain fields with primitive types, like strings, integers, etc. The WCF service will serialize these into XML or JSON.
On the client side, jQuery will be your best friend. I personally prefer JSON because the properties of your objects are much easier to get at that way instead having to deal with parsing a bunch of XML. So, setup your service to output JSON.
Also, to make your service URLs easier to read, make sure to use a RESTful approach. It's as easy as decorating your service methods with the WebGet attribute and supplying a UriTemplate. Once you see some examples, it'll blow your mind. Note: if you ever encounter a WebInvoke with Method="GET", just use WebGet instead...it's more compact...no Method specification needed.
This particular article was EXTREMELY useful to me when I was developing my WCF service and the ASP.NET app that consumed it: http://www.c-sharpcorner.com/UploadFile/sridhar_subra/116/
Here's another person asking the same question as you: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/879d46af-9c78-4b5d-b746-82843d742a6f
Hope this helps! Long live WCF!
With .NET 3.5 your best bet is WebHttpBinding which accepts plain old XML (POX) and you need to send XML to the WCF service.
You can also use WCF REST using REST starter kit. For samples have a look here. This supports JSON as well.
If you were using .NET 4.0, JSON-enabled WCF HTTP was the way to go. WCF REST with 4.0 was an alternative although I really do not like it.

Making an ASP.NET ASMX web service easily callable by ColdFusion web clients

I've written an ASP.NET webservice (.asmx style rather than WCF) and I have the main web methods working now in my prototype enviroment. Now I need to add several new parameters to one of the webmethods but, more significantly, I need to make my webservice as "consumable" (callable) as possible from a ColdFusion web application. I'm confused about how to best design parameter types for the webservice methods in light of usage by CF (or other client callers).
My prototype relies on my own ASP.NET web page which calls my webservice; here is how the proxy class for my service is called from the .aspx page:
BrokerASMXProxy.Svc.FileService brokerService = new BrokerASMXProxy.Svc.FileService();
recNumber = brokerService.UploadFile(binData, fileNameOnly, kvData);
Here is the signature of the webmethod:
[WebMethod]
public string UploadFile(byte[] incomingArray
, string FileName
, MetaData[] metaDataArray)
I'm a little concerned already about the byte array and the MetaData array (will ColdFusion have a problem calling this service with argument types like that?). I assume strings are not a problem but what are the best practices for designing webmethods (and their types) for use by ALL types of callers?
p.s. I also have a DownloadFile webmethod that has "out" parameters:
[WebMethod]
public bool DownloadFile(string recNumString, out byte[] docContents, out string returnFiletype)
..and I wonder similarly about ColdFusion's accomodation of these outputs. Thank you in advance.
If you're running ColdFusion 8, and happen to have that on the same physical server as your .NET code, you may be better off using .NET integration, where you can write a .NET class and intantiate it inside your CF code as if it were a CFC or a Java object.
Here's a good example (with tons more on the same blog) of writing a .NET class that's used in CFML code.
However, if your .NET and CF app servers are not on the same machine, you may have to go with a web service as you originally thought.
The great thing about web services is that they are language agnostic. It looks like you have a MetaData type or class defined, and you're expecting a collection of those to be sent as an input parameter. What that's going to ultimately translate to is some XML that represents that array, and each item in the array will be broken down to its core parts (strings, numbers, bools, etc)... In theory, it should just work, as long as you design your input XML accordingly.
It's been a while since I last created a .NET web service, but I'm sure there's a way to look at the expected XML structure (aka WSDL). Find that, and you've got your answer. Then you just need to create XML that fits that definition from your ColdFusion code and you're all set.

Best approach for passing XML to a webservice?

I have XML files in a directory that I wish to get over to a webservice on a server that will validate them and return a true/false as to whether they are valid in construct and values etc.
Reason for server side processing is that the validation rules may change from time to time and need to adjust in one place as opposed to all client machines.
What would be the best mechanism to do this?
At the moment am wondering if passing the XMLDocument object from the client to the webservice as a parameter may be the way to go?
Am using .net 3 for this in C#
You might consider using something like a WCF service and streaming the xml up using the GZipStream. I am doing something similar to this and it is working pretty well.
Wouldn't a normal string be enough? It's overkill to serialize / deserialize an entire XDoc instance in my opinion. Of course, you can also zip the entire thing to reduce the size of the requests.
Depending on your validation rules, it might be more sensible to encapsulate them in something like an XML schema hosted on a public URL.
That way, clients can validate against the schema in one line of code, rather than having to connect to a web service.

Categories

Resources