如何使用jquery通过ajax发送对象?

时间:2016-04-30 06:00:06

标签: jquery ajax asp.net-mvc

我尝试通过ajax将WagonAvaliableResponse对象发送到方法。当我测试此错误时 TypeError:WagonAvaliableResponse不是函数

如何解决这个问题?

  var WagonAvaliableResponse = {
            UniqId:'',
            checksum:'',
            FromStation:0,
            ToStation:0,
            MainUniqId:0.0,
            compartmentCapicityField:'',
            isCompartmentField:'',
            circularNumberSerialField:'',
            countingAllField:'',
            rateCodeField:0,
            airConditioningField:false,
            mediaField:false,
            timeOfArrivalField:'',
            rationCodeField:0,
            soldcountingField:0,
            ownerField: ''
        };

 $('#Continue').click(function(e) {
            e.preventDefault;
            var obj = new WagonAvaliableResponse();
            obj.Uniqid = $('#DepartureTrain article.show input[data=UniqId]').val();
            obj.mainuniqId = $('input[name=MainUniqId]').val();
            $.ajax({
                url: '/Train/CheckValidSelection',
                data: obj,
                type: "POST",
                success: function (result) {
                    alert(result.Message);
                },
                error: function () {
                    alert(result.Message);
                }
            });
        });

1 个答案:

答案 0 :(得分:0)

使用$.post代替$.ajax,您还可以使其更清晰,更具可读性,请参阅下文。

$('#Continue').click(function(e) {
  e.preventDefault;
  var obj = WagonAvaliableResponse;
  obj.Uniqid = $('#DepartureTrain article.showinput[data=UniqId]').val();
  obj.mainuniqId = $('input[name=MainUniqId]').val();
  $.post('/Train/CheckValidSelection', obj)
    .done(function() {
      alert( "Success" );
    })
    .fail(function() {
      alert( "error" );
    })