POSTing JSON data to WCF REST

Posted by Randall Sexton on Stack Overflow See other posts from Stack Overflow or by Randall Sexton
Published on 2010-06-02T19:20:07Z Indexed on 2010/06/02 19:24 UTC
Read the original article Hit count: 320

Filed under:
|
|

I'm trying to send data from a client application using jQuery to a REST WCF service based on the WCF REST starter kit.

Here's what I have so far.

Service Definition:

    [WebHelp(Comment = "Save PropertyValues to the database")]        
    [WebInvoke(Method = "POST", UriTemplate = "PropertyValues_Save",
               BodyStyle = WebMessageBodyStyle.WrappedRequest,
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public bool PropertyValues_Save(Guid assetId, Dictionary<Guid, string> newValues) { ... }

Call from the client:

$.ajax({
    url:SVC_PROPERTYVALUES_SAVE,
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: jsonData,
    dataType: "json",
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus + ' ' + errorThrown);
      },
    success: function(data) {
        if (data)  {
            alert('Values saved'); $("#confirmSubmit").dialog('close'); 
        }
        else {
            alert('Values failed to save'); $("#confirmSubmit").dialog('close');
        }           
    }
});

Example of the JSON being passed:

{ "assetId": "d70714c3-e403-4cc5-b8a9-9713d05b2ee0", "newValues": [ { "key": "bd01aa88-b48d-47c7-8d3f-eadf47a46680", "value": "0e9fdf34-2d12-4639-8d70-19b88e753ab1" }, { "key": "06e8eda2-a004-450e-90ab-64df357013cf", "value": "1d490aec-f40e-47d5-865c-07fe9624f955" } ] }

I'm using Windows Authentication on the virtual directory. When I call operations that are GETs, everything is fine. This code is prompting the browser to log in. When I enter my credentials, I simply get an alert in my browser which says "error undefined".

Even if you can't help my specific error, do you see anything that looks wrong from glancing?

I've been beating my head on this nearly all day.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about wcf

Related posts about JSON