无法让我的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,这是网络检查员的输出:
http://localhost:4252/LoggingTest HTTP信息
请求方法:POST
状态代码:400 Bad Request
请求标题
接受: /
缓存控制:max-age = 0
内容类型:application / x-www-form-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-Requested-With:XMLHttpRequest
表格数据
消息:测试消息
响应标头
内容长度:1165
内容类型:text / html
日期:2010年7月1日星期四18:56:15 GMT
服务器:Microsoft-HTTPAPI / 1.0
答案 0 :(得分:2)
所以,我刚刚完成了这个,接口:
[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);
}
答案 1 :(得分:1)
尝试在服务合同上添加以下行,我认为你应该使用裸露的WrappedRequest
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
查看此post以获取更多装饰
答案 2 :(得分:1)
这可能只是让它为你工作的难题的一部分,但这引起了我的注意:
您可能需要仔细检查您的JSON语法,我认为它需要是变量和变量名称的双引号字符串。
e.g。
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { message: "test message" });
}
需要:
function jqueryPost() {
var url = "/LoggingTest";
$.post(url, { "message": "test message" });
}
n.b。双引号“message”
编辑:感谢下面的反馈,这里有一些可能对格式化JSON有用的链接: