经过复杂类型的数据,jQuery的阿贾克斯后复杂、类型、数据、阿贾克斯后

2023-09-10 13:23:33 作者:心作怪

我的数据模型类看起来如下:

My data model class looks as below:


[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操作契约是:

My WCF operation contract is:


[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
string SaveOrder(Order order);

我的Ajax调用是这样的:

My ajax call looks like:


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:错误的请求状态。这是头看起来像谷歌浏览器:

But when I make this call, I get a 400: bad request status. This is what the header looks like in 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

请让我知道,我可以纠正这一点。

Please let me know how I can rectify this.

谢谢, 阿伦

推荐答案

有在获得这个解决两个问题。 1.我的服务本身的一些错误(这是我的坏) 2,我不得不做JSON.stringify()的orderJson对象。

There were two issues in getting this resolved. 1. Some bug in my service itself (this is my bad) 2. I had do to JSON.stringify() on the orderJson object.


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: JSON.stringify(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
            RedirectToMvcApp(result);
        },
        error: function (request, error) {// When Service call fails
            alert('Service call failed: ' + request.status + ' ' + request.statusText);
        }
    });
}