简体   繁体   中英

How to send Cross Domain Ajax POST with Laravel

I am trying to send a cross-domain post call using AJAX from one of my Laravel sites, to another.

The other topic I saw addressed the first issue I was running into with Access Control headers: Jquery: Cross-domain ajax 'POST' with laravel

I am getting a 419 error, implying that I am not using a CSRF token, but with either token I use (local token or other domain token) it doesn't work.

var CSRF_TOKEN = {{ csrf_token() }};
$.ajaxSetup( { headers : { 'X-CSRF-TOKEN' : CSRF_TOKEN } } );

var tracking_id = "{{ isset( $tracking_id ) ? $tracking_id : 'test-20' }}";
$.ajax({
    type: 'POST',
    url: 'https://example.com/beacon',
    crossDomain: true,
    data: { 'tracking_id': tracking_id },
    success: function(responseData, textStatus, jqXHR) {
        console.log( 'Click!' );
    },
    error: function (responseData, textStatus, errorThrown) {
        console.log( responseData );
    }
});

Excluding the route in VerifyCsrfToken.php would be your easiest bet. You can then make a middleware or some other means to restrict the request by ip, oauth, etc.

Docs: https://laravel.com/docs/5.7/csrf#csrf-excluding-uris

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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