I have a WPF application that exposes a REST WCF service (via WebServiceHost
) with a contract that looks something like this (simplified):
[ServiceContract]
public interface IItemServiceContract
{
[WebGet(UriTemplate = "Items/{id}")]
Item GetItem(string id);
[WebGet(UriTemplate = "Items")]
IEnumerable<Item> GetItems();
[WebInvoke(UriTemplate = "Items", Method = "PUT")]
IEnumerable<Item> CreateItems(IEnumerable<Item> list);
}
When I navigate to http://localhost:8070/Contoso/Services/Items/ItemService/Items
with a browser, I get a response that looks something like this:
<ArrayOfItem xmlns="http://schemas.datacontract.org/2004/07/Contodo.Services.Items" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Item>
<ItemState>
<Comment i:nil="true"/>
<DeactivationTime>0001-01-01T00:00:00</DeactivationTime>
<Id>18f1a5e4-a94a-4f37-a533-3a75a10e7373</Id>
<IsSpecial>false</IsSpecial>
</ItemState>
<ItemTypeId>10</ItemTypeId>
<HelpInfo/>
<Identity>Ident1</Identity>
<AdditionalInfo>
<?xml version="1.0" encoding="utf-16"?>
<Content>
<SpecialContent />
</Content></AdditionalInfo>
<TextParameter>kjhdfsjh kj dkfjg kj</TextParameter>
<UserName i:nil="true"/>
</Item>
</ArrayOfItem>
What would be an easy and friction-free approach to consume this service with JavaScript? How can a client quickly build the http requests and the appropriate XML for it?
I am fairly in the Html5/javaScript world but in a C# I would have an API in place that is centered around the Item
object that gets serialized to XML. But is that the way to go for here?
Update:
Based on the first comments and answers, it seems that XML is not the ideal format for a JavaScript/webbrowser consumer but I can't just change the format to JSON because that would probably break existing clients that already rely on this XML format. So ideally I would do REST content type negotiation and put/get JSON or XML. But can this be done with WCF REST services?