Using jQuery to communicate to an Ajax-enabled WCF Service
WCF Service Definition using object response
[OperationContract]
[WebInvoke(Method = "POST")]
public TestResponse TestService(string input)
{
try
{
TestResponse response = new TestResponse() { Message = string.Format("TestService
result is {0}", input) };
return response;
}
}
JQuery Service Call using object
$.ajax({
type: "POST",
data: '{"input":"16"}',
contentType: "application/json; charset=utf-8",
success: function (data) {
var tmp = data.d;
alert(‘Message: ‘ + tmp.Message);
},
error: ServiceCallFailed
});
---------------------------------------------------------------------------------------------
WCF Service Definition using string response
[OperationContract]
[WebInvoke(Method = "POST")]
public string TestService(string input)
{
try
{
TestResponse response = new TestResponse() { Message = string.Format("TestService
result is {0}", input) };
JavaScriptSerializer js = new JavaScriptSerializer(); //using
System.Web.Script.Serialization;
string json =
js.Serialize(response);
return json;
}
}
JQuery Service Call using parse
$.ajax({
type: "POST",
data: '{"input":"16"}',
contentType: "application/json; charset=utf-8",
success: function (data) {
var response = $.parseJSON(data.d);
alert('Message: ' +
response.Message); },
error: ServiceCallFailed
});
No comments:
Post a Comment