0
votes

I am trying to send data as json from localhost to a website domain.com and I see that jquery can't send it when using cross-domains;

Then I read about jsonp and now I don't know how to write the code;

Somehow I need to replace the code from the news function with a jsonp call;

<script>
    function news(data) {
        $.ajax({
            'url': 'http://domain.com/index.php/api/news',
            'data': {'data': data},
            'success': function(data) {
            },
            dataType: 'json'
        });
    }
    function unique(data) {
        $.ajax({
            'url': 'http://localhost/fb-group/index.php/api/unique',
            'data': {'data': data},
            'success': function(data) {
                if (data && (data.status === 1) && data.news) {
                    news(data.news);
                }
            },
            dataType: 'json'
        });
    }
    access_token = 'token';
    $.ajax({
        'url': 'https://graph.facebook.com/453240374771082?fields=feed&method=GET&format=json&suppress_http_code=1&access_token=' + access_token,
        'data': '',
        'success': function(data) {
            if (data && data.feed && data.feed.data) {
                allData = data.feed.data;
                $.each(allData, function(index, value) {
                    unique(value);
                });
            }
        },
        dataType: 'json'
    });
</script>

is this ok on the server side?

this is how i catch , parse and save the date received thru ajax call

    public function actionNews() {
        $data = json_encode($_GET['data']);
        $data_decoded = json_decode($data);
//        print_r($data_decoded);
        $model_feedM = new FeedMLive();
        $model_feedM->data_id = $data_decoded->id;
        $model_feedM->data_from_name = $data_decoded->from->name;
        $model_feedM->data_from_id = $data_decoded->from->id;
        $model_feedM->data_to_data_name = $data_decoded->to->data[0]->name;
        $model_feedM->data_to_data_id = $data_decoded->to->data[0]->id;
        $model_feedM->data_message = strlen($data_decoded->message) > 0 ? $data_decoded->message : 'unknown';
        $model_feedM->data_picture = isset($data_decoded->picture) ? $data_decoded->picture : 'unknown';
        $model_feedM->data_link = isset($data_decoded->link) ? $data_decoded->link : 'unknown';
        $model_feedM->views = 0;
        $model_feedM->created = time();
        $model_feedM->status = FeedM::ACTIVE;
        if ($model_feedM->validate() && $model_feedM->save())
            echo '{"status_live":"1"}';
        else
            echo '{"status_live":"0"}';
        exit;
    }
1
Just change dataType: 'json' to dataType: 'jsonp'? Note that JSONP is just adding a script element to the DOM, so your data will be sent via the data keyword in the query string. Of course the server must also read the value from the URL. - Felix Kling

1 Answers

1
votes
<script>
function news(data) {
    $.ajax({
        'url': 'http://domain.com/index.php/api/news?callback=?',
        'data': {'data': data},
        'success': function(data) {
        },
        dataType: 'json'
    });
}
function unique(data) {
    $.ajax({
        'url': 'http://localhost/fb-group/index.php/api/unique?callback=?',
        'data': {'data': data},
        'success': function(data) {
            if (data && (data.status === 1) && data.news) {
                news(data.news);
            }
        },
        dataType: 'json'
    });
}
access_token = 'token';
$.ajax({
    'url': 'https://graph.facebook.com/453240374771082?fields=feed&method=GET&format=json&suppress_http_code=1&access_token=' + access_token+'&callback=?',
    'data': '',
    'success': function(data) {
        if (data && data.feed && data.feed.data) {
            allData = data.feed.data;
            $.each(allData, function(index, value) {
                unique(value);
            });
        }
    },
    dataType: 'json'
});

Server gets 'callback' param and return data include value of 'callback' param, This is cross-domains by jquery!