http://ranaictiu-technicalblog.blogspot.com/2011/09/sharepoint-2010-access-wcf-service-with.html?showComment=1323864446083#c4340503928745769810 has an example of using JQuery Ajax to GET or POST to call a WCF Rest service. It uses cksdev.
I have done the same and can GET or POST to a web service. I now want to send data back to the web service and do some work on it.
So I have tried the following
IWCF.cs original
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<string> SearchList();
IWCF.cs Updated
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<string> SearchList(string test);
I have added a parameter to the Operation Contract
WCF.svc.cs original
public List<string> SearchList()
{
var list = new Code.List();
return list.SearchResults("test");
}
WCF.svc.cs updated
public List<string> SearchList(string test)
{
var list = new Code.List();
return list.SearchResults(test);
}
I have added a parameter to the List() method and use that to search my content.
JS file original
try {
$.ajax({
type: "POST",
url: '/_vti_bin/WCF.svc/SearchList',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
WCFServiceGetSucceeded(msg);
},
error: WCFServiceGetFailed
});
} catch (e) {
alert('error invoking service.get()' + e);
}
JS file updated
try {
$.ajax({
type: "POST",
url: '/_vti_bin/WCF.svc/SearchList',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{"test": "test"}',
success: function (msg) {
WCFServiceGetSucceeded(msg);
},
error: WCFServiceGetFailed
});
} catch (e) {
alert('error invoking service.get()' + e);
}
I have added data: '{"test": "test"}',. This now gives me a 400 error.
How do I post data back to the WCF endpoint?