First, since you don't want to depend on Microsoft Ajax ScriptManager, don't use <enableWebScript /> in the endpointBehaviors/behavior. It is Microsoft-specific JSON.
Fortunately, however, WCF makes it very easy to allow your client to decide whether they want XML or generic JSON.
Use the <webHttp /> behavior.
<endpointBehaviors>
<behavior name="My.WcfServices.webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
Create a custom WebServiceHost and custom property attribute as described in
Damian Mehers' blog, WCF REST Services. In Mehers' code, the type is determined by the request content type. You may want to extend it to examine the URL, for example, .xml or .json or ?format=xml|json.
In the SerializeReply method, examine the URL.
Message request = OperationContext.Current.RequestContext.RequestMessage;
Uri url = request.Properties["OriginalHttpRequestUri"] as Uri;
// Examine ?format query string
System.Collections.Specialized.NameValueCollection colQuery = System.Web.HttpUtility.ParseQueryString(url.Query);
string strResponseFormat = colQuery["format"];
// or examine extension
string strResponseFormat = url.LocalPath.Contains(".json") ? "json" : "xml";
Define your method(s)
[OperationContract]
[WebGet(UriTemplate="Hello.{responseFormat}")] // or "Hello?format={responseFormat}"
[DynamicResponseType]
public string Hello(string responseFormat)
{
return "Hello World";
}
Example URLs:
http://localhost/myrest.svc/Hello.xml
http://localhost/myrest.svc/Hello.json
or
http://localhost/myrest.svc/Hello?format=xml
http://localhost/myrest.svc/Hello?format=json
- Both JSON and XML are easy to consume across browsers. Libraries, such as jQuery for JSON and Sarissa for XML make it even easier.
NOTE: If you see error "Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding.", add the baseAddressPrefixFilters element and add localhost (or whatever your domain) to IIS Host Header Names.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://localhost"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>