通过JQuery使用WCF POST进行400错误请求HTTP响应

Tho*_*yme 7 c# wcf jquery post httpwebrequest

无法让我的JQuery POST被WCF服务接受.这是来自javascript的POST:

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

这是我通过接口接受POST的方式:

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

并实施:

public void LoggingTest(string message)
{
    log.Debug(message, null);
}
Run Code Online (Sandbox Code Playgroud)

当我调用函数jqueryPost时,我在Web检查器中看到了400 Bad Request的HTTP响应.不确定如何让POST请求工作.

(在7/1上添加)
@James,这是web检查器的输出:

http:// localhost:4252/LoggingTest HTTP信息
请求方法:POST
状态码:400错误请求
请求标头
接受:/
Cache-Control:max-age = 0
内容类型:application/x-www-form-urlencoded
原产地:http:// localhost:4252
Referer:http:// localhost:4252 /
User-Agent:Mozilla/5.0(Windows; U; Windows NT 5.1; C - )AppleWebKit/532.4(KHTML,如Gecko)Qt/4.6.2 Safari/532.4
X-Requested-With:XMLHttpRequest
表单数据
消息:测试消息
响应标头
内容长度:1165
内容类型:text/html
日期:2010年7月1日星期四18:56:15 GMT
服务器:HTTPAPI/1.0

Tho*_*yme 2

所以,我最终这样做了,接口:

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

执行:

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在它起作用了。一定与 UriTemplate 中传递的参数有关,因为当我将其更改为传递参数时,如下所示:

UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}",
Run Code Online (Sandbox Code Playgroud)

它开始接受 POST。

7/7 编辑:这也是最终的 JavaScript:

jqueryPost('LoggingTest/LogID/debug?errorCode=0', { message: 'this is a test message'} ;

function jqueryPost(url, message) {
    $.post(url, message);
}
Run Code Online (Sandbox Code Playgroud)