Saravanan M P
Saravanan M P

Reputation: 29

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.

Upvotes: 0

Views: 1603

Answers (3)

Sridhar R
Sridhar R

Reputation: 20408

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);
            }

        });
    });

});

Upvotes: 1

Nouphal.M
Nouphal.M

Reputation: 6344

As per jQuery Docs

 "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of 
your URL to specify the callback. Disables caching by appending a query string parameter,  "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

Try

$(function() {

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

    $('.idlink').on('click', function() {
        $.ajax({
            url: diffDomainUrl,
            type: "POST",//if not specified get is the default
            dataType: 'jsonp',
            data: {}, //send data to server as key value pair if any eg {id:20}
            jsonpCallback: 'mycallback'
        });
    });

});

And your callback function

 function mycallback(responseJSON){
       ........
    }

Upvotes: 0

Let me see
Let me see

Reputation: 5094

change
$_GET['jsoncallback']; to

$_GET['callback'];

Upvotes: 1

Related Questions