Having trouble getting my JQuery POST to be accepted by the WCF Service. Here's the POST from the javascript:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
This is how I'm accepting the POST, via an Interface:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/LoggingTest",
BodyStyle = WebMessageBodyStyle.Bare)]
void LoggingTest(string message);
And the implementation:
public void LoggingTest(string message)
{
log.Debug(message, null);
}
When I call the function jqueryPost I see in the web inspector an HTTP response of 400 Bad Request. Not sure how to get the POST request to work.
(Added on 7/1)
@James, here is the output from the web inspector:
http://localhost:4252/LoggingTest HTTP Information
Request Method:POST
Status Code:400 Bad Request
Request Headers
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, like 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
So, I just ended up doing 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. Must have something to do with the parameters being passed in the UriTemplate, because when I changed it to pass the parameters like so:
UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}",
it started accepting the POST.
Edit 7/7: Here's the final JavaScript also:
jqueryPost('LoggingTest/LogID/debug?errorCode=0', { message: 'this is a test message'} ;
function jqueryPost(url, message) {
$.post(url, message);
}
Try adding following line on service contract, also I think you should use WrappedRequest insted of Bare
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
look into this post for more decorations
It might be only one part of the puzzle to get it working for you, but this caught me out for sometime:
You may need to double check your JSON syntax, I think it needs to be double quoted strings around both the variable and variable name.
e.g.
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
needs to be:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { "message": "test message" });
}
n.b. double quotes "message"
EDIT: Thanks for the feedback below, here a few links that might prove useful for formatting JSON:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With