2010-06-30 164 views
7

无法让我的JQuery POST被WCF服务接受。下面是从JavaScript中的POST:400通过JQuery使用WCF POST的错误请求HTTP响应

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我在网络检查员看到一个HTTP响应400错误请求。不知道如何让POST请求工作。

(由7/1)
@詹姆斯,这里是从网上督察输出:

http://localhost:4252/LoggingTest HTTP信息
请求方法:POST
状态代码:400错误的请求
请求头
接受:/
缓存控制:max-age = 0
内容类型:应用/ X WWW的窗体-urlencoded
产地:http://localhost:4252
的Referer:
的User-Agent:Mozilla的/ 5.0(视窗; U; Windows NT 5.1; Ç - )为AppleWebKit/532.4(KHTML,例如Gecko)QT/4.6.2 Safari浏览器/ 532.4
X-请求-随着:XMLHttpRequest的
表单数据
消息:测试消息
响应头
内容长度:1165
的Content-Type:text/html的
日期:星期四,2010年7月1日18时56分15秒GMT
服务器:HTTPAPI/1.0

+0

你可以使用Fiddler(或者其他的东西)并发布完整的请求/响应吗?我想知道它是否发布到一个不正确的网址(例如取决于是否部署到根目录) – 2010-06-30 05:17:38

+0

詹姆斯,我已经发布了上面的网络检查器输出。 – ThoughtCrhyme 2010-07-01 19:00:23

回答

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); 
} 
+0

我想知道,你可以发布你的最终Javascript,以便我们可以查看解决方案的端到端。 – 2010-07-02 20:48:14

+0

周二我回到办公室后我会发布,谢谢Mark的兴趣。 – ThoughtCrhyme 2010-07-04 05:28:27

+0

这是一种方法来传递一个没有uritemplate字符串值,在后身? – enguerran 2012-07-02 15:42:22

1

尝试增加服务合同上以下行,也我认为你应该使用WrappedRequest insted的裸的

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

了解这一post为更多的装饰

+0

我已经尝试在实现中允许AspNetCompatibilityRequirementsMode(不能将其添加到接口)。我已经在Bare和Wrapped请求之间切换。虽然我在网上看到了帮助很多其他人的地方...可悲的是没有运气。 – ThoughtCrhyme 2010-06-30 07:13:12

+0

你正在使用什么配置,你可以在这里粘贴配置吗?你有没有检查过我已经给过参考? – IBhadelia 2010-07-01 04:21:56

+0

我没有使用IIS,所以没有.config – ThoughtCrhyme 2010-07-01 20:54:07

1

这可能是得到它为你工作难题的只是其中的一部分,但是这抓住了我出去了一段时间:

您可能需要仔细检查您的JSON语法,我认为它需要在变量和变量名称周围使用双引号字符串。

例如

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

需要是:

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

注:双引号 “消息”


编辑:谢谢你下面的反馈,这里几个环节可能证明是有用的格式化JSON:

+0

-1:该对象不是“JSON”,它只是另一个JavaScript对象。 JS对解析更为宽松,不需要双引号。 – 2011-06-01 13:28:02

+0

嗨@Matti Virkkunen,我看到你从哪里来......我同意JavaScript本身更“宽松”,但数据交换格式(即不需要JavaScript)的JSON更加严格。 www.JSON.org建议使用双引号。但说实话,我找不到一个ECMAScript 5权威信息工作草案的好链接,但http://developer.yahoo.com/yui/json/(关于JSON格式)非常权威。请记住,JSON语法不是JavaScript说的(记住,JSON可以与JavaScript一起使用),它只是一种基于/受javacript子集启发的格式。 – 2011-06-01 14:53:22

+0

@Matti Virkkunen说完上述所有内容后,确实如此,您可以避免在值/字符串对中使用双引号。 jquery.post文档中的示例没有它们。实际上,jquery.post文档甚至没有说明你需要使用JSON。但无论如何,我认为它不值得-1,特别是因为服务器端的c#需要双引号。 http://stackoverflow.com/questions/4939620/can-wcf-accept-json-encoded-using-single-quotes-and-non-quoted-identifiers如果我已经帮助澄清的事情,请随时提出你的 - 1,如果不是无后顾之忧。 – 2011-06-01 14:58:53