2011-03-22 111 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