400 Bad Request HTTP Response using POST WCF via jQuery

Cannot get my jQuery POST to be accepted by WCF. Here's the POST from javascript:

function jqueryPost() { var url = "/LoggingTest"; $.post(url, { message: "test message" }); } 

This is how I accept POST through the interface:

 [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/LoggingTest", BodyStyle = WebMessageBodyStyle.Bare)] void LoggingTest(string message); 

And implementation:

 public void LoggingTest(string message) { log.Debug(message, null); } 

When I call the jqueryPost function, I see the 400 Bad Request response in the web inspector. Not sure how to get a POST request.

(Posted on 7/1)
@James, here is the output of the web inspector:

http: // localhost: 4252 / LoggingTest HTTP information Request method: POST
Status Code: 400 Bad Request
Request headers
To accept:/
Cache-control: max-age = 0
Content-Type: application / x-www-form-urlencoded
Origin: http: // localhost: 4252
Referer: http: // localhost: 4252 /
User-Agent: Mozilla / 5.0 (Windows; U; Windows NT 5.1; C -) AppleWebKit / 532.4 (KHTML, e.g. Gecko) Qt / 4.6.2 Safari / 532.4
X-Requested-With: XMLHttpRequest
Form data
message: test message
Response Headers
Content Length: 1165
Content-Type: text / html
Date: Thu, 01 Jul. 2010 18:56:15 GMT
Server: microsoft-httpapi / 1.0

+7
jquery c # post wcf
source share
3 answers

So, I just finished this, Interface:

 [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}", BodyStyle = WebMessageBodyStyle.Bare)] void LoggingTest(string logID, string logLevel, int errorCodeInt, Stream message); 

Implementation:

 public void LoggingTest(string logID, string logLevel, int errorCodeInt, Stream message) { switch (logLevel) { case "error": log.Error(errorCodeInt, message, null); break; case "warn": log.Warn(errorCodeInt, message, null); break; case "info": log.Info(errorCodeInt, message, null); break; case "debug": log.Debug(errorCodeInt, message, null); break; } } 

And now it works. Something must be related to the parameters passed to the UriTemplate, because when I changed it to pass such parameters:

 UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}", 

he started taking POST.

Editing 7/7: here's the latest JavaScript too:

 jqueryPost('LoggingTest/LogID/debug?errorCode=0', { message: 'this is a test message'} ; function jqueryPost(url, message) { $.post(url, message); } 
+2
source

Try adding the following line to the service contract, also I think you should use the WrappedRequest insted of Bare

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

view post for more decorations

+1
source

It may be just one piece of the puzzle to make it work for you, but it sometimes caused me:

You may have to double check your JSON syntax, I think it should be double-quoted strings around the variable name and variable.

eg.

 function jqueryPost() { var url = "/LoggingTest"; $.post(url, { message: "test message" }); } 

should be:

 function jqueryPost() { var url = "/LoggingTest"; $.post(url, { "message": "test message" }); } 

nb double quotes "message"


EDIT: Thanks for the feedback below, here are some links you might find useful for formatting JSON:

+1
source

All Articles