passing complex type as data to jquery ajax post

前端 未结 1 1470
盖世英雄少女心
盖世英雄少女心 2021-02-11 03:49

My data model class looks as below:

[DataContract]
public class Order
{
    [DataMember]
    public string Id { get; set; }
    [DataMember]
    public string Additio         


        
相关标签:
1条回答
  • 2021-02-11 04:38

    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);
            }
        });
    }
    

    0 讨论(0)
提交回复
热议问题