gpt4 book ai didi

c# - 通过 JQuery 使用 WCF POST 的 400 错误请求 HTTP 响应

转载 作者:太空狗 更新时间:2023-10-29 20:32:20 25 4
gpt4 key购买 nike

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

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

这就是我通过接口(interface)接受 POST 的方式:

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

和实现:

public void LoggingTest(string message)
{
log.Debug(message, null);
}

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

(7 月 1 日添加)
@James,这是网络检查器的输出:

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

最佳答案

所以,我只是这样做了,界面:

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

实现:

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

现在可以了。必须与 UriTemplate 中传递的参数有关,因为当我将其更改为传递参数时,如下所示:

UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}",

它开始接受 POST。

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

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

function jqueryPost(url, message) {
$.post(url, message);
}

关于c# - 通过 JQuery 使用 WCF POST 的 400 错误请求 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3146329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com