Я бы определил конечную точку JSON в WCF, как подсказывает @marc_s. Затем я бы использовал jQuery для вызова службы и JSON2.js для строкового запроса.
Вот пример HTML-страницы, которую вы могли бы легко вывести как часть вашего рабочего процесса (обратите внимание, ничего не генерируется) ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JSON Demo</title>
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="json2.js" type="text/javascript"></script>
<script type="text/javascript">
// You could set this value when you generate the HTML
var SERVICE_URL = "http://localhost:48788/JsonDemoService.svc";
$(document).ready(function () {
$('#GoButton').click(function () {
// Construct a customer object (you could pass pure JSON
// but I prefer to use objects and then stringify at the end)
var customer = {
name: $('#Name').val()
};
$.ajax({
type: 'POST',
url: SERVICE_URL + "/AddCustomer",
data: JSON.stringify(customer),
success: function () {
// Handle a successful return here
},
error: function (xhr, thrownError) {
// Handle an error calling the service here
alert(thrownError);
},
contentType: "application/json",
dataType: "html"
});
});
});
</script>
</head>
<body>
<input id="Name"/>
<input id="GoButton" type="button" value="Test"/>
</body>
</html>
Как видите - довольно просто отправлять объекты в службу WCF. Для завершения картины здесь есть услуга ...
Реализация сервиса
using System.Runtime.Serialization;
using System.ServiceModel;
namespace Demo
{
[DataContract]
public class Customer
{
// Use the Name parameter to specify a lowercase name
// so that it looks like Javascript on the client and
// c# on the server
[DataMember(Name = "name")]
public string Name { get; set; }
}
[ServiceContract]
public interface IJsonDemoService
{
[OperationContract]
void AddCustomer(Customer customer);
}
public class JsonDemoService : IJsonDemoService
{
public void AddCustomer(Customer customer)
{
// Add the customer here
}
}
}
.svc file
<%@ ServiceHost Service="Demo.JsonDemoService" %>
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings />
<services>
<service name="Demo.JsonDemoService">
<endpoint address=""
behaviorConfiguration="json"
binding="webHttpBinding"
name="jsonEndpoint"
contract="Demo.IJsonDemoService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Важные части в конфигурации привязки - именно это говорит WCF ожидать JSON.