简体   繁体   English

Asp.net Mvc:Jquery post array + anti伪造令牌

[英]Asp.net Mvc: Jquery post array + anti forgery token

how do I post an array to an action on my controler with the anti forgery token. 如何使用防伪令牌将数组发布到我的控制器上的操作。

This is my Jquery postdata: 这是我的Jquery postdata:

var postData = { '__RequestVerificationToken': $('input[name=__RequestVerificationToken]').val(), 'productIds': IDs };

this is my Jquery post: 这是我的Jquery帖子:

$.post("MyProducts/DeleteProduct" , postData, function(data) { });

This is my action: 这是我的行动:

public void DeleteProduct(List<int> productIds)
    {
        foreach (int i in productIds)
        {
            _repository.DeleteProduct(i, null);
        }        
    }

I also use an object to store my anti forgery token and I wonder how I can use that with the postdata. 我还使用一个对象来存储我的防伪标记,我想知道如何将它与postdata一起使用。

This is the token object: 这是令牌对象:

var token = { '__RequestVerificationToken': $('input[name=__RequestVerificationToken]').val() };

Kind regards 亲切的问候

var ids = [1,2];

var data = {
__RequestVerificationToken : token,
productIds : ids
};

$.post(url, data, function() ...

where token is the var you mentioned 其中token是你提到的var

Assuming you have all your product IDs in the HTML it would be much easier to use jqueryForm plugin : 假设您在HTML中拥有所有产品ID,那么使用jqueryForm插件会更容易:

$("form").ajaxSubmit({url: "MyProducts/DeleteProduct", success: function(response) {
  // Handle the response
}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM