2011-03-22 107 views
2

我有方法,有添加客户:WCF。 REST架构。如何从客户端获取对象JSON(POST)

[WebInvoke(UriTemplate = "/", Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
    [OperationContract] 
    string CreateCustomer(Customer customer); 

,我有一个问题,因为这种方法的所有调用失败! 与GET相同的方法很好地工作(只需在url中返回Customer对象bu请求标识)。从WCF服务 响应:

Status: 400 

headers = { 
     "Cache-Control" = private; 
     "Content-Length" = 2900; 
     "Content-Type" = "text/html"; 
     Date = "Tue, 22 Mar 2011 12:51:09 GMT"; 
     Server = "Microsoft-IIS/7.0"; 
     "X-Aspnet-Version" = "4.0.30319"; 
     "X-Powered-By" = "ASP.NET"; 
    } 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Request Error</title> 
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style> 
    </head> 
    <body> 

,我不能调试方法,因为问题在方法步骤之前occups。

我想用我的第一个参数客户的客户这个问题 怎么办POST吧?以及如何使用它(服务器上解析json)?

在提琴手

enter image description here

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> 
<HTML><HEAD><TITLE>Bad Request</TITLE> 
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> 
<BODY><h2>Bad Request - Invalid Header</h2> 
<hr><p>HTTP Error 400. The request has an invalid header name.</p> 
</BODY></HTML> 

,但我不知道这是像客户一样的问题有!

+2

有一个名为Fiddler的工具,可以查看客户端和服务器之间发送了什么,我在为WCF REST服务开发PoC时使用了这个工具 - 这非常有用:http ://www.fiddler2.com/fiddler2/ – 2011-03-22 10:57:05

+1

我使用它。并不知道什么是错的! – Evgeny 2011-03-22 10:58:15

回答

2

我不完全知道你做了什么问题,但是当我看着这个,我有一个不同的页眉:

Content-Type = application/xml

而且,你似乎已经从剁掉你的身体问题,这可能是有用的。

1

来自客户请求的内容类型无效。必须是应用程序/ json