ValidateAntiForgeryToken

ValidateAntiForgeryToken

我的[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");

                }
            });

最佳答案

我认为这与打包数据的混合有关。尝试将所有内容放入一个对象:

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)

关于jquery - ValidateAntiForgeryToken错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33518677/

10-12 00:56