Kriuchko
10/29/2019 - 10:12 AM

Ajax


\!h Ajax query jQuery
jQuery.ajax({
    method: 'get',
    url: '<?php echo $block->getUrl('prospapay/index/calculate') ?>',
    data: { price: price, form_key: FORM_KEY }
}).success(function(transport) {
    jQuery('.prospapay-instalment-amount').text(transport);
});

\!h Ajax query prototype
new Ajax.Request('<?php echo $block->getUrl('prospapay/index/calculate') ?>', {
    method: 'get',
    parameters: {price: price, form_key: FORM_KEY},
    dataType: "html",
    onSuccess: function(transport) {
        if (transport.status == 200){
            jQuery('.prospapay-instalment-amount').text(transport.responseText);
            checkMinMaxAmount(price);
        }
    }
});
                
\!h Ajax query listener magento 2 product add to cart exmaple
jQuery(document).ajaxComplete(function (event, xhr, settings) {
    if (settings.url.indexOf("customer/section/load/?sections=cart") > 0) {
      cartObj = xhr.responseJSON;
      console.log(cartObj);
      console.log('ex tax: ' + cartObj.cart.subtotal_excl_tax);
      console.log('inc tax: ' + cartObj.cart.subtotal_incl_tax);
      console.log('subtotal: ' + cartObj.cart.subtotal);
    }
});

\!h How do I send a cross-domain POST request via JavaScript

https://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript

https://zinoui.com/blog/cross-domain-ajax-request

\!h Cross-origin request

https://www.moxio.com/blog/12/how-to-make-a-cross-domain-request-in-javascript-using-cors

https://jvaneyck.wordpress.com/2014/01/07/cross-domain-requests-in-javascript/

<script type="text/javascript">
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://some.com/shopify-get-customer.php');
  xhr.send();
</script>

<?php
header('Access-Control-Allow-Origin: *'); /* * - allowed for all domains */
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

echo "Hello";