views:

291

answers:

3

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

+1  A: 

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

IBhadelia
I have tried allowing the AspNetCompatibilityRequirementsMode in the implementation (you can't add it to an interface). And I've switched between Bare and Wrapped requests. Although I've seen online where that helps a lot of other people... sadly no luck with that.
ThoughtCrhyme
What configuration you are using, can you please paste configuraiton here? have you checked post i have given for reference?
IBhadelia
I'm not using IIS so there is no .config
ThoughtCrhyme
A: 

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);

}

ThoughtCrhyme
I wonder, could you post your final Javascript so we can view the end to end of your solution.
Mark Schultheiss
I'll post it when I get back into the office on Tuesday, thanks for the interest though Mark.
ThoughtCrhyme
A: 

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"

Alex Key