Search code examples
javascriptphpjqueryjsonp

Send data to remote server using jsonp


Here is My code: server A

$(function() {

        var diffDomainUrl = 'http://domain_B.com/analtyics/cookie.php?jsoncallback=mycallback';

    $('.idlink').on('click', function() {
        $.ajax({
            url: diffDomainUrl,
            dataType: 'jsonp',
            data: {},
            success: function (data, textStatus) {
                console.log(textStatus);
                console.log(data);
            },
            jsonpCallback: 'mycallback'
        });
    });

});

and server B

<?php
$_GET['jsoncallback'];
if(isset($_GET['jsoncallback']))
{
setcookie("T_LNG",$_GET['jsoncallback'],strtotime('+30 days'));

echo $_COOKIE['T_LNG']."Welcome";
} ?>

in this code i m not getting anything. i don't know whthere its working or not or my method is wrong.


Solution

  • Your url contain call back already so dont set that in ajax remove and try remove this jsonpCallback: 'mycallback'

    Try this

    $(function() {
    
            var diffDomainUrl = 'http://domain_B.com/analtyics/cookie.php?jsoncallback=mycallback';
    
        $('.idlink').on('click', function() {
            $.ajax({
                url: diffDomainUrl,
                dataType: 'jsonp',
                data: {},
                success: function (data, textStatus) {
                    console.log(textStatus);
                    console.log(data);
                }
    
            });
        });
    
    });