简体   繁体   中英

POSTing JSON data to WCF REST

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" 
        } 
    ] 
}

web.config

<?xml version="1.0"?>
<configuration>

     <configSections>

          <sectionGroup name="applicationSettings"
                        type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

          </sectionGroup>

     </configSections>

 <system.web>
  <compilation debug="true"/>
        </system.web>
 <system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
 </system.serviceModel>

     <appSettings>

         ...

     </appSettings>

</configuration>

I'm using Windows Authentication on the virtual directory and anonymous access is disabled. 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.

If you have authentication problems it can be important to verify that only "Windows Authentication" is switches on and "Anonymous" access is switches of in your virtual directory.

If you do have no problem with irtual directory your should post the code of your web.config file.

From your question it stay not clear for me whether you have only authentication problems or the PropertyValues_Save will be not called with the JSON data which you posted. Could you formulate more clear this?

UPDATED : It seems to me that your program will work if the JavaScript client will use "Key" and "Value" property names instead of "key" and "value".

I recommend you also look at the end on my old answer How do I build a JSON object to send to an AJAX WebService? to verify, that you pack the data correct for the call of server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM