how to send ASCsrf token via ajax?

Closed
Advanced Security - PHP Register/Login System Advanced Security - PHP Register/Login System April 28, 2018
Login to reply
Milos Stojanovic Support Agent
5 years ago

Hey,

You need to include js-bootstrap.php script before your custom code, not after it. 

So, just make sure that it is included the js-bootstrap.php after the jQuery and but before your custom code and it should work without any issues. :)

jeckziel cortes
5 years ago
Already read the documentation, applied your solution but it does not change the error.

<script>
    var priceDoll = document.getElementById('priceDoll');
    
    priceDoll.onkeyup = function(){
    var e = document.getElementById("coin");
    var strCoin = e.options[e.selectedIndex].text;
    var coin_value;
    /* Replace ./data.json with your JSON feed */
    fetch('https://api.coinmarketcap.com/v1/ticker/' + strCoin + '/?convert=USD').then(response => {
      return response.json();
    }).then(data => {
      for (var i = 0; i < data.length; i++) {
            var coin_value = data[i].price_usd;
            document.getElementById('coinPriceLabel').innerHTML = priceDoll.value/coin_value;
            coin_value = priceDoll.value/coin_value;
            
           /* var xhttp = new XMLHttpRequest();
            xhttp.open("POST", "coin_post1.asp", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send(coin_value);
          */
            var request = $.ajax({
                type: "POST",
                url: "uploadItemInfo.php",
                data: {coin_value},
                //dataType: "html"
            });
            request.done(function(msg) {
              alert ( "Response: " + msg );
            });
            request.fail(function(jqXHR, textStatus) {
              alert( "Request failed: " + textStatus );
            });
        }
    }).catch(err => {
      // Do something for an error here
        document.getElementById('coinPriceLabel').innerHTML = "Sorry, can't fetch data from server";
    });
    }
    
    //Changes value when selectbox is changed
    function cryptoPrice(){
        var priceDoll2 = document.getElementById('priceDoll');
        var e = document.getElementById("coin");
        var strCoin = e.options[e.selectedIndex].text;
        var coin_value;
        
        // Replace ./data.json with your JSON feed
        fetch('https://api.coinmarketcap.com/v1/ticker/' + strCoin + '/?convert=USD').then(response => {
          return response.json();
        }).then(data2 => {
          for (var i = 0; i < data2.length; i++) {
                var coin_value2 = data2[i].price_usd;
                document.getElementById('coinPriceLabel').innerHTML = priceDoll2.value/coin_value2;
                coin_value = priceDoll.value/coin_value;
              
              var request = $.ajax({
                type: "POST",
                url: "uploadItemInfo.php",
                data: {coin_value},
                //dataType: "html"
            });
            request.done(function(msg) {
              alert ( "Response: " + msg );
            });
            request.fail(function(jqXHR, textStatus) {
              alert( "Request failed: " + textStatus );
            });
              
          }
        }).catch(err => {
          // Do something for an error here
            document.getElementById('coinPriceLabel').innerHTML = "Sorry, can't fetch data from server";
        });
    }
</script>
<script src="ASLibrary/js/js-bootstrap.php"></script>
jeckziel cortes
5 years ago

My code is this: 

var request = $.ajax({
    type: "POST",
    url: "uploadItemInfo.php",
    data: {coin_value,<?= ASCsrf::getTokenName() ?>,<?= ASCsrf::getToken() ?>},
    //dataType: "html"
});
request.done(function(msg) {
  alert ( "Response: " + msg );
});
request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});


My question is how to post the token to make AS recognize it and let the script send data?

thanks in advance!