[英]400 Bad Request HTTP Response using a WCF POST via JQuery
无法让我的JQuery POST被WCF服务接受。 这是来自javascript的POST:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
这是我通过接口接受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,这是web检查器的输出:
http:// localhost:4252 / LoggingTest HTTP信息
请求方法:POST
状态代码:400错误请求
请求标题
接受: /
缓存控制:最大年龄= 0
内容类型:应用/ X WWW的窗体-urlencoded
来源: http:// localhost:4252
推荐人: 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-要求 - 由于:XMLHttpRequest的
表格数据
消息:测试消息
响应标题
内容长度:1165
内容类型:text / html的
日期:2010年7月1日星期四18:56:15 GMT
服务器: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);
}
尝试在服务合同上添加以下行,我认为你应该使用Bare的WrappedRequest
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
看看这篇文章 ,了解更多装饰品
它可能只是让它为你工作的谜题的一部分,但这让我感到有些困惑:
您可能需要仔细检查您的JSON语法,我认为它需要是变量和变量名称的双引号字符串。
例如
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
需要是:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { "message": "test message" });
}
nb双引号“消息”
编辑:感谢下面的反馈,这里有一些链接可能对格式化JSON有用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.