I've been searching tirelessly online trying to figure out how to properly send a JSON string to a WCF service. I have multiple successful GET requests in my application, but I just can't seem to get the POST request to work - I keep getting a 400 error in the Chrome browser.
Here is an example of my code:
JS:
workDataAsJson = JSON.stringify('{"TestData":"121"}');
$.ajax({
type: "POST",
async: true,
cache: false,
timeout: webCallDefaultTimeout,
contentType: "application/json; charset=utf-8",
url: baseUrl.concat('UpsertWorkData/' + workDataAsJson),
dataType: "json",
success: function (response, status, jqXHR) {
if (status == 'success') {
var workplanData = $.parseJSON(response);
// notify user of success,...
} else {
displayGenericModal('Web Service Error', 'Uh Oh! Unable to Connect to the Database to Obtain Work Data');
}
},
error: function (response, status, jqXHR) {
displayGenericModal('Web Service Error', 'Uh Oh! Unable to Connect to the Database to Obtain Work Data');
}
});
} catch(ex) {
alert(ex);
}
Now for the WCF operation contract,...
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", UriTemplate = "UpsertWorkData/{WorkData}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string UpsertWorkData(string WorkData);
Trying to configure the web.config file has also been a journey. Here are some snippets:
<?xml version="1.0"?>
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
(Additional configuration settings here)
</configuration>
If anyone can offer guidance or solutions, it would be greatly appreciated. Thank you!