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?

link|improve this question

62% accept rate
feedback

3 Answers

You must provide UriTemplate like: UriTemplate = "/SearchList?test={test}"

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/SearchList?test={test}" BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<string> SearchList(string test);

Maybe it is not the whole solution, but you can start there.

link|improve this answer
Thanks. Had a quick try but did not solve the problem. Think I need to read up on WCF more to understand what is going on. – John Dec 14 '11 at 14:43
Try to remove one of the methods. So you have only one and clean method. Why don't you use WebGet instead of WebInvoke (POST)? – Anatoly Mironov Dec 14 '11 at 15:05
Sadly it is a copy and paste learning job rather then knowing what I am doing. I am trying to read up on WCF as we speak so hopefull ycan work it out. Maybe I should have just used ASMX like I used to – John Dec 14 '11 at 15:10
feedback

Instead of '{"test": "test"}' use {"test": "test"}(remove single quote)

link|improve this answer
feedback

I was also unable to get any POST to work. I kept getting errors back telling me my properties were invalid.

I finally realized that all of the other columns in my list started with capital letters, except for the one that I was inserting.

When I renamed my column from 'data' to 'Data', and updated my JSON payload accordingly, it worked just fine.

As I can't see your payload, I'm not sure if this'll solve your problem, but it worked for me.

For completeness, here's a snippet of my basic Jquery call:

var inputData = JSON.stringify(YOUR_JSON_OBJECT);

$.ajax({
  type: 'POST',
  url: YOUR_PATH,
  contentType: 'application/json',
  processData: false,
  dataType: 'json',
  data: inputData,
  success: function (response, status, jqXHR) {
    console.log("POST succeeded", arguments);        
  },
  error: function () {
    console.log("POST failed", arguments);
  }
});
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.