[英]400 Bad Request HTTP Response using a WCF POST via JQuery
Having trouble getting my JQuery POST to be accepted by the WCF Service. 无法让我的JQuery POST被WCF服务接受。 Here's the POST from the javascript:
这是来自javascript的POST:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
This is how I'm accepting the POST, via an Interface: 这是我通过接口接受POST的方式:
[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 . 当我调用函数jqueryPost时,我在Web检查器中看到了400 Bad Request的HTTP响应。 Not sure how to get the POST request to work.
不确定如何让POST请求工作。
(Added on 7/1) (在7/1上添加)
@James, here is the output from the web inspector: @James,这是web检查器的输出:
http://localhost:4252/LoggingTest HTTP Information
http:// localhost:4252 / LoggingTest HTTP信息
Request Method:POST请求方法:POST
Status Code:400 Bad Request状态代码:400错误请求
Request Headers请求标题
Accept: /接受: /
Cache-Control:max-age=0缓存控制:最大年龄= 0
Content-Type:application/x-www-form-urlencoded内容类型:应用/ X WWW的窗体-urlencoded
Origin: http://localhost:4252来源: http:// localhost:4252
Referer: http://localhost:4252/推荐人: 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.4User-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:XMLHttpRequestX-要求 - 由于:XMLHttpRequest的
Form Data表格数据
message:test message消息:测试消息
Response Headers响应标题
Content-Length:1165内容长度:1165
Content-Type:text/html内容类型:text / html的
Date:Thu, 01 Jul 2010 18:56:15 GMT日期:2010年7月1日星期四18:56:15 GMT
Server:Microsoft-HTTPAPI/1.0服务器:HTTPAPI / 1.0
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中传递的参数有关,因为当我更改它以传递参数时,如下所示:
UriTemplate = "LoggingTest/{logID}/{logLevel}?errorCode={errorCodeInt}",
it started accepting the POST. 它开始接受POST。
Edit 7/7: Here's the final JavaScript also: 编辑7/7:这也是最终的JavaScript:
jqueryPost('LoggingTest/LogID/debug?errorCode=0', { message: 'this is a test message'} ;
function jqueryPost(url, message) {
$.post(url, message);
}
Try adding following line on service contract, also I think you should use WrappedRequest insted of Bare 尝试在服务合同上添加以下行,我认为你应该使用Bare的WrappedRequest
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
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. 您可能需要仔细检查您的JSON语法,我认为它需要是变量和变量名称的双引号字符串。
eg 例如
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
needs to be: 需要是:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { "message": "test message" });
}
nb double quotes "message" nb双引号“消息”
EDIT: Thanks for the feedback below, here a few links that might prove useful for formatting JSON: 编辑:感谢下面的反馈,这里有一些链接可能对格式化JSON有用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.