2013-07-02 29 views
0

我的URL路徑組件太長,我試圖將方法從GET更改爲POST。我遇到了反序列化問題。這裏是服務WCF JSON Web服務,從GET到POST的問題

public class AppUser : IAppUser 
{ 
    //[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddShop/{shop}")] 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate = "AddShop")] 
    List<fme.AppUserResult> services.IAppUser.AddShop(string shop) 
    { 
     JavaScriptSerializer jss = new JavaScriptSerializer(); 
     fme.AppUser oUser = jss.Deserialize<fme.AppUser>(shop); 

     return oUser.Add();   
    }  
} 

,這裏是請求(它與GET的工作,如果它是在260個字符)

{"shopCity":"City","shopState":"State","shopPhone":"Phone","shopName":"Name","shopEmail":"Email","shopHours":"Hours","shopZip":"Zip","shopAddress":"Street"} 

我也曾嘗試

{"shop":{"shopCity":"City","shopState":"State","shopPhone":"Phone","shopName":"Name","shopEmail":"Email","shopHours":"Hours","shopZip":"Zip","shopAddress":"Street"}} 

這是錯誤:

The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'shopCity' from namespace ''.'. See server logs for more details. The exception stack trace is:

回答

2

它可以是該方法會自動嘗試將json反序列化爲參數的類型。因爲它不能將json反序列化爲字符串類型的對象它的錯誤。嘗試改變參數類型fme.AppUser,而不是

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate = "AddShop")] 
List<fme.AppUserResult> services.IAppUser.AddShop(fme.AppUser oUser) 
{ 
    return oUser.Add();   
} 
+0

就是這樣,感謝您的幫助。 – fizgig