AntiForgeryToken issue with jquery post

Topics: General, Writing modules
Mar 11, 2011 at 11:13 PM

I keep getting this "A required anti-forgery token was not supplied or was invalid."

I have tried using both @using (Html.BeginFormAntiForgeryPost()) and @Html.AntiForgeryTokenOrchard() but I still have the error.

var token = { '__RequestVerificationToken': $('input[name=__RequestVerificationToken]').val() };     
                $.ajax({
                    type: "POST",
                    url: $('#CreatAddressUrl').val(),
                    dataType: "json",
                    contentType: 'application/json; charset=utf-8',
                    data: { __RequestVerificationToken: token, model: json },
                    success: function (data) {
                        
                    },
                    error: function (xhr, status, error) {
                        alert(status);
                    }
                });

Mar 12, 2011 at 12:20 AM

I got past this by using this jquery plugin: http://weblogs.asp.net/dixin/archive/2010/05/22/anti-forgery-request-recipes-for-asp-net-mvc-and-ajax.aspx

then using this in my javascript:

var json = JSON.stringify(address);
            var token;
            json = $.appendAntiForgeryToken(json, token);
            $.post($('#CreatAddressUrl').val(), json, AddressCreated);

Apr 4, 2011 at 9:59 PM

Great, that worked for me as well. Is there anything like this in the jQuery library included with Orchard?

Apr 5, 2011 at 9:32 AM

jakvike - can you post an example of the controller that you are using to process the JSON data? Mine is going screwy but I don't know if I'm doing it right...

Apr 5, 2011 at 7:19 PM

What is going screwy with it?  Are you hitting the Controller's Method?

Apr 5, 2011 at 7:21 PM

Ah, I think I know what it is. Controller method was being called but the arguments were null, I think I've got my head around it now :)

Jan 12, 2012 at 5:17 PM
Edited Jan 12, 2012 at 5:19 PM

I also used Dixin's lib to do ajax map update in a module ago.

i found another solution,  ref  

http://orchard.codeplex.com/discussions/276058

which one is better?

Jan 17, 2012 at 3:25 AM
technics wrote:

jakvike - can you post an example of the controller that you are using to process the JSON data? Mine is going screwy but I don't know if I'm doing it right...

 

Reference this : http://pastebin.com/58LyvsgA