Мой класс модели данных выглядит следующим образом:
[DataContract]
public class Order
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string AdditionalInstructions { get; set; }
[DataMember]
public double TotalAmount { get; set; }
[DataMember]
public DateTime OrderDate { get; set; }
[DataMember]
public Customer Customer { get; set; }
[DataMember]
public OrderedProduct OrderedProduct { get; set; }
}
[DataContract]
public class OrderedProduct
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public double Price { get; set; }
[DataMember]
public int Quantity { get; set; }
}
[DataContract]
public class Customer
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
Мой контракт операции WCF:
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
string SaveOrder(Order order);
Мой вызов ajax выглядит следующим образом:
function SaveOrder() {
var orderJson = {
AdditionalInstructions: $("span:first").text(),
Customer: { FirstName: $("#firstName").val(), LastName: $("#lastName").val() },
OrderedProduct: { Id: $("#productList").val(), Quantity: $("#quantity").val() }
};
// the post to your webservice or page
$.ajax({
type: "POST", //GET or POST or PUT or DELETE verb
url: "http://localhost:14805/OrderService.svc/SaveOrder", // Location of the service
data: orderJson, //Data sent to server
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: false, //True or False
success: function (result) {//On Successfull service call
alert(result);
},
error: function (result) {// When Service call fails
alert('Service call failed: ' + result.status + ' ' + result.statusText);
}
});
}
Но когда я звоню, я получаю статус 400: плохой запрос.Вот как выглядит заголовок в Google Chrome:
Request URL:http://localhost:14805/OrderService.svc/SaveOrder
Request Method:POST
Status Code:400 Bad Request
Request Headers
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:165
Content-Type:application/json; charset=UTF-8
Host:localhost:14805
Origin:http://localhost:14805
Referer:http://localhost:14805/index.html
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.127 Safari/534.16
X-Requested-With:XMLHttpRequest
Request Payload
AdditionalInstructions=dbl-click+here+to+add+notes!&Customer%5BFirstName%5D=asdf&Customer%5BLastName%5D=fdsa&OrderedProduct%5BId%5D=3&OrderedProduct%5BQuantity%5D=12
Response Headers
Cache-Control:private
Connection:Close
Content-Length:1647
Content-Type:text/html
Date:Mon, 07 Mar 2011 19:14:57 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
Пожалуйста, дайте мне знать, как я могу это исправить.
Спасибо, Арун