2015-11-04 52 views
3

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

       } 
      }); 
+1

你會得到什麼錯誤? –

+1

請僅爲您的字符串使用英文 –

+0

我的錯誤是: 所需的防僞表單字段__RequestVerificationToken不存在 –

回答

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) 
相關問題