ValidateAntiForgeryToken错误

时间:2015-11-04 09:54:38

标签: jquery ajax asp.net-mvc

我的[ValidateAntiForgeryToken]属性存在问题。

我通过jQuery将__RequestVerificationToken发送到服务器,但服务器响应错误。

我传递给服务器的数据是两个类,当我只发送一个类时,请求成功完成。

这是我的代码:

var form = $('#__AjaxAntiForgeryForm');
            var token = $('input[name="__RequestVerificationToken"]', form).val();

            var ProductRegisterVm =
                {
                    "Price": $scope.Product.Price,
                    "CanSend": $scope.Product.CanSend,
                    "Changeability": $scope.Product.Changeability,
                    "CanGiveBack": $scope.Product.CanGiveBack,
                    "IsExist": $scope.Product.IsExist,
                    "BrandCode": 1,
                    "WarrantyCode": 1,
                    "MadeIn": $scope.Product.MadeIn,
                    "Description": $scope.Product.Description
                };


            var ProductAttrbiute =
                {
                    "FramesColor": $scope.Product.FramesColor,
                    "FramesMaterial": $scope.Product.FramesMaterial,
                    "LensMaterial": $scope.Product.LensMaterial,
                    "LensColor": $scope.Product.LensColor,
                    "IsForMale": $scope.Product.IsForMale,
                    "IsSunny": $scope.Product.IsSunny

                };


            $.ajax({
                url: '/Product/PostRegister',
                type: 'POST',
                contentType: "application/json",
                dataType: "json",
                data: '__RequestVerificationToken:'+token+","+JSON.stringify({ productRegisterVm: ProductRegisterVm, productAttrbiute: ProductAttrbiute }),

                success: function (data) {
                    alert(data.success);

                },
                error: function () {
                    alert("ERROOOOOOOR");

                }
            });

1 个答案:

答案 0 :(得分:1)

我认为这与你收集数据的混合有关。尝试将所有内容放在一个对象中:

var data = {
    ProductRegisterVm =
            {
                "Price": $scope.Product.Price,
                "CanSend": $scope.Product.CanSend,
                "Changeability": $scope.Product.Changeability,
                "CanGiveBack": $scope.Product.CanGiveBack,
                "IsExist": $scope.Product.IsExist,
                "BrandCode": 1,
                "WarrantyCode": 1,
                "MadeIn": $scope.Product.MadeIn,
                "Description": $scope.Product.Description
            },
    ProductAttrbiute =
            {
                "FramesColor": $scope.Product.FramesColor,
                "FramesMaterial": $scope.Product.FramesMaterial,
                "LensMaterial": $scope.Product.LensMaterial,
                "LensColor": $scope.Product.LensColor,
                "IsForMale": $scope.Product.IsForMale,
                "IsSunny": $scope.Product.IsSunny

            },
     _RequestVerificationToken = $('input[name="_RequestVerificationToken"]', form).val()
};

data : JSON.stringify(data)