While
using webHttpBinding in wcf service we have to send json data.Wcf internally uses
the DataContractSerializer
to convert json data into
objects.
WcfService:-
namespace WebHttpBindingXmlHttpRequestWcf
{
[ServiceContract]
public interface IService
{
[OperationContract]
Person GetData(Person objPerson);
}
{
[ServiceContract]
public interface IService
{
[OperationContract]
Person GetData(Person objPerson);
}
[DataContract]
public
class Person
{
[DataMember]
public
string Name { get;
set; }
[DataMember]
public
string Age { get;
set; }
}
}
namespace WebHttpBindingXmlHttpRequestWcf
{
public class Service : IService
{
public Person GetData(Person objPerson)
{
return objPerson;
}
}
}
{
public class Service : IService
{
public Person GetData(Person objPerson)
{
return objPerson;
}
}
}
<system.serviceModel>
<services>
<service name="WebHttpBindingXmlHttpRequestWcf.Service">
<endpoint address="webHttpBinding"
<service name="WebHttpBindingXmlHttpRequestWcf.Service">
<endpoint address="webHttpBinding"
binding="webHttpBinding"
contract="WebHttpBindingXmlHttpRequestWcf.IService"
behaviorConfiguration="webHttpBindingEndpointBehavior">
</endpoint>
</service>
</services>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBindingEndpointBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Javascript File:-
//call the wcf service
function callservice(objPerson)
{
//url
of the service
var
url=’’;
//here
the name(objPerson) must match with the name in the operation contract
var data = '{"objPerson":' +
$.toJSON(objPerson) + '}';
//jquery
ajax function to call wcf service
$.ajax({ type: 'POST',
async: true,
url: url,
contentType: 'application/json;
charset=utf-8',
data: data,
success: function
(res) { alert(‘success...’); },
error: function
(err) { alert(‘error occurred...’); }
});
}
Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com
No comments:
Post a Comment