views:

43

answers:

2

I am looking to create a SOAP API using C# which I can then call using JavaScript. I use C# regularly but do not have any experience with creating API's. I would like to call the API using JavaScript as this will be used to submit form data from multiple websites not maintained by us.

If there is a better solution than SOAP I am open to suggestions.

If anyone can point me to examples or has any examples they can share I would appreciate it.

TIA BrianKE

EDIT: I should have mentioned that I would to deploy a solution that will allow form data from multiple websites, not under our domain, to submit data directly to our database, hence the API. Perhaps there is a different way to handle this other than an API that I am not aware of.

A: 

If there is a better solution than SOAP?

If you are going to consume the API from javascript JSON is preferred. You may checkout this example which illustrates how to expose a JSON enabled WCF service for consumption from jquery AJAX.

Darin Dimitrov
I am not sure this will work as the web page that would call the API is not located on the same server as the API, unless I am misunderstanding the 'same origin policy'.
BrianKE
Well then you could use [JSON-P](http://remysharp.com/2007/10/08/what-is-jsonp/) or a server side script which will act as bridge. For `JSON-P` to work with WCF you might need a custom binding. Here's an [example on MSDN](http://msdn.microsoft.com/en-us/library/cc716898.aspx) and a [blog post](http://jasonkelly.net/2009/05/using-jquery-jsonp-for-cross-domain-ajax-with-wcf-services/) on this subject. You should be aware that JSON-P works only with GET requests which limits the amount of data you could send as request.
Darin Dimitrov
JSON-p is dangerous, it could allow the script from a different server to run malicious code on your page. If you have complete control over the other server, go ahead, otherwise, you should create a bridge to the other server from your server.
Juan Mendes
A: 

Yes. You can.

Barring small issues/restrictions with XHR, it's perfectly "fine" to consume SOAP services from JS. The biggest issue is "dealing" with the XML, which can be more cumbersome than JSON (but it isn't bad if you use the correct tools such as XPath extractors). For my projects I use a small wrapper setup for the AJAX/XHR/SOAP call (not WSDL generated) that can take custom encoding/decoding functions.

Google searches shows several promising results/examples including JavaScript SOAP Client.

Note: If you need cross-domain access there are several methods including a proxy or the newer cross-domain XHR support (however, these require client and/or server support) or, depending on client, just really relaxed settings. These cross-domain considerations are generally no different than if using "REST" or other web-service APIs.

pst