How to make ajax request with anti-forgery token in mvc

前端 未结 5 1635
时光取名叫无心
时光取名叫无心 2020-12-05 00:47

I have problem with below details from MVC project.

When I am trying to use jquery ajax request with loading panel like spinning gif (or even text), I am getting err

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 01:09

    Have you added your token to the header of the ajax call?

    You need to add AntiForgeryToken in your message header in the ajax call:

    var token = $('input[name="__RequestVerificationToken"]').val();
    
    var headers = {};
    
    headers['__RequestVerificationToken'] = token;
    
    $.ajax({
            url: ... some url,
            headers: headers,
            ....
    });
    

    Try this in your code:

    var token = $('input[name="__RequestVerificationToken"]').val();
    var tokenadr = $('form[action="/TransportJobAddress/Create"] input[name="__RequestVerificationToken"]').val(); 
    
    var headers = {};
    var headersadr = {};
    headers['__RequestVerificationToken'] = token;
    headersadr['__RequestVerificationToken'] = tokenadr;
    
    $('#submitaddress').click(function subaddr(event) {
        event.preventDefault();
        event.stopPropagation();
      //$('#addAddress').html(' Sending...');
       // $('#addAddress').blur();
        //  $(this).bl
        if ($('#Jobid').val()!="") {
            $('#TransportJobId').val(parseInt($('#Jobid').val()));
            $.ajax(
                  {
                      url: '/TransportJobAddress/create',
                      type: 'POST',
                      headers:headersadr, 
                      data: "__RequestVerificationToken=" + token + "" + $('form[action="/TransportJobAddress/Create"]').serialize(),
                      success: function poste(data, textStatus, jqXHR) { $('#addAddress').html(data); return false; },
                      error: function err(jqXHR, textStatus, errorThrown) { alert('error at address :' + errorThrown); }
                  });
        }
        else {
            var transportid = 2;
            $.ajax({
                url: '/TransportJob/create',
                type: 'POST',
                headers:headers, 
                data: $('form[action="/TransportJob/Create"]').serialize(),
                success: function sfn(data, textStatus, jqXHR) {
                    transportid = parseInt(data);
                    $('#Jobid').val(data);
                   // alert('inserted id :' + data);
                    $('#TransportJobId').val((transportid));
                    $.ajax(
             {
    
                 url: '/TransportJobAddress/create',
                 type: 'POST',
                 //beforeSend: function myintserver(xhr){  
                 //        $('#addAddress').html('
    please wait ...
    '); //}, headers:headers, data: $('form[action="/TransportJobAddress/Create"]').serialize(), success: function poste(data, textStatus, jqXHR) { $('#addAddress').html(data); }, error: function err(jqXHR, textStatus, errorThrown) { alert('error at address :' + errorThrown); } }); }, error: function myfunction(jqXHR, textStatus, errorThrown) { alert("error at transport :" + jqXHR.textStatus); }, complete: function completefunc() { // alert('ajax completed all requests'); return false; } }); } });

    Added headers line in your ajax call.

提交回复
热议问题