Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

c# - 400 Bad Request HTTP Response using a WCF POST via JQuery

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...