Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
When doing any web service development, ALWAYS use Fiddler (www.fiddler2.com) to watch what is happening between your browser and the server. This will save you a LOT of time when debugging and troubleshooting. –  James Love Jan 5 '13 at 13:07
add comment

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.

share|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
add comment

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

share|improve this answer
    
This is incorrect, if you remove the single quotes the payload becomes a JavaScript Object and if you used it in the payload for the ajaxQuery, it'll come back as something like "[object Object]". –  James Love Jan 5 '13 at 13:09
add comment

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);
  }
});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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