2013-03-12 40 views
1

我有一些樣品JSON我要張貼到我的WebAPI FO測試,但我不能見得到它deserilaize我的目標,我進入。此外,從形式測試中的WebAPI一個帖子用JSON

這裏是我的形式:

<form action="/api/orders" method="post" > 
       <textarea type="text" id="order" name="order"></textarea> 
       <input type="submit"/> 
</form> 

這裏是我的方法:

[HttpPost] 
     public HttpResponseMessage Post(Model.Order order) 
     { 
      var response = Request.CreateResponse<Order>(HttpStatusCode.Created, order); 
      string uri = Url.Link("DefaultApi", new { id = order.OrderID }); 
      response.Headers.Location = new Uri(uri); 
      return response; 
     } 

以下是我在貼的形式提交:

{ 
    "ShipToContactPhone" : "9999999999", 
    "ContactPhone" : "9999999999", 
    "IsPlaced" : 0, 
    "ContainerName" : null, 
    "Terms" : "3", 
    "ShipDate" : null, 
    "OrderGUID" : "6D55FB6E-36A2-43F3-8316-2BE740ACC8E1", 
    "CatalogName" : null, 
    "ShipToCountry" : "USA", 
    "SalesRepID" : 0, 
    "OrderType" : null, 
    "ShipVia" : null, 
    "IsContainer" : null, 
    "BillingAddress1" : "TEST ACCOUNT", 
    "BillingCity" : "BROOK", 
    "CustomerID" : 721182, 
    "BillingAddress2" : "", 
    "OrderStatus" : "Accept", 
    "TaxTotal" : 0, 
    "Source" : null, 
    "Discount" : 0, 
    "OrderDate" : "3\/8\/13", 
    "CustomerNumber" : "26", 
    "OrderTotal" : 2160, 
    "ShippingTotal" : 0, 
    "ShipToZip" : "07663", 
    "CurrencyCode" : "USD", 
    "TaxRate" : 0, 
    "Origin" : null, 
    "CancelDate" : null, 
    "WrittenByName" : "(null)", 
    "BillingState" : "NJ", 
    "Exported" : null, 
    "ShipToAddress1" : "6 ANYWHERE STREET", 
    "ShipToAddress2" : "", 
    "Notes" : "", 
    "FreightTerm" : null, 
    "ShipToContactEmail" : "[email protected]", 
    "ContactEmail" : "[email protected]", 
    "ShipToFax" : "9999999999", 
    "ContactName" : "", 
    "PONumber" : "", 
    "ShipToContactName" : null, 
    "WrittenForName" : null, 
    "CatalogCode" : null, 
    "ManufacturerID" : 3456, 
    "ShipToCompanyName" : "TEST", 
    "ShipToState" : "NJ", 
    "isDefault" : 1, 
    "PriceLevel" : 5, 
    "ShipToCity" : "BROOK", 
    "ContainerCubes" : 0, 
    "WrittenForRepnumber" : null, 
    "MarketName" : "", 
    "DoNotShipBefore" : null, 
    "RepNumber" : null, 
    "FOB" : null, 
    "IsDeleted" : null, 
    "SubTotal" : 0, 
    "ContactFax" : "9999999999", 
    "BillingZip" : "07663", 
    "CustomerName" : "TEST", 
    "ExportDate" : null, 
    "OrderID" : 0, 
    "WrittenForRepNumber" : null, 
    "BillingCountry" : "USA" 
} 

我認爲這可能是因爲內容類型不是application/json來自窗體。

我該如何簡單地測試這個並使其工作?

+0

你的班級'Order'是怎麼樣的? – 2013-03-12 16:09:13

+0

簡單的類,在 – Slee 2013-03-12 16:38:44

回答

1

嘗試使用jQuery.post函數。還請看this answer

+0

以上添加了一些示例數據是的,JQuery將是一個不錯的選擇。 Web瀏覽器使用form-url編碼提交表單。 – 2013-03-12 16:53:47