7

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

Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
ThoughtCrhyme
  • 517
  • 3
  • 8
  • 18

3 Answers3

2

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);
}
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
ThoughtCrhyme
  • 517
  • 3
  • 8
  • 18
1

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
  • 291
  • 1
  • 5
  • 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 Jun 30 '10 at 07:13
  • What configuration you are using, can you please paste configuraiton here? have you checked post i have given for reference? – IBhadelia Jul 01 '10 at 04:21
  • I'm not using IIS so there is no .config – ThoughtCrhyme Jul 01 '10 at 20:54
1

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:

Community
  • 1
  • 1
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • -1: That object is not "JSON", it's just another JavaScript object. JS is more lenient about the parsing and double quotes are not required. – Matti Virkkunen Jun 01 '11 at 13:28
  • Hi @Matti Virkkunen, I see where your coming from...... I agree JavaScript itself IS more "lenient", but JSON, the data-interchange format (i.e. not requiring JavaScript) is stricter. www.JSON.org suggests the use of double quotes. But to be honest I cannot find a good link to the ECMAScript 5 working draft for authoritative information but http://developer.yahoo.com/yui/json/ (About the JSON format) is pretty authoritative. Remember that JSON syntax isn't JavaScript per-say (remember that JSON can be used with javascript) it's just a format based upon / inspired by a subset of javacript. – Alex KeySmith Jun 01 '11 at 14:53
  • @Matti Virkkunen Having said all that above, it's true you can probably get away with not using double quotes for value / string pairs. The examples on jquery.post documentation don't have them. And in fact the jquery.post documentation doesn't even state you need to use JSON. But either way, I don't think it deserves a -1, especially as the c# on the server-side requires the double quotes. http://stackoverflow.com/questions/4939620/can-wcf-accept-json-encoded-using-single-quotes-and-non-quoted-identifiers If I've helped to clarify things please feel free to amened your -1, if not no worries. – Alex KeySmith Jun 01 '11 at 14:58
  • Came across this, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON and made me remember this post, it's one of the best JSON descriptions I've come across. – Alex KeySmith Sep 20 '11 at 17:18
  • 1
    The point of my comment was that your code snippet has *nothing to do with JSON whatsoever*. The object there is just passing string key/value pairs to jQuery as a plain JavaScript object, which will send them as separate POST fields, not JSON. Claiming the stricter parsing rules of JSON have any relevance here is just wrong. To make it even clearer: that is not going to send "{\"message\": \"test message\"}" to the server, it's going to send "message=test%20message". – Matti Virkkunen Sep 21 '11 at 11:34
  • Ah I see! Sorry I completely misnterpretted your initial comment. I thought that you commented upon javascript objects vs json and double quotes. Very good point (on looking at it with new eyes) on my rubbish example, you're right I'm not even using JSON! Ha ha! – Alex KeySmith Sep 21 '11 at 11:43
  • Double quotes in the JSON attributes made a difference in my project. – Ignacio Hagopian Aug 12 '16 at 18:32