问题描述
我正在尝试将 json
中的数据从 localhost
发送到网站域。 com
我发现 jquery
在使用跨域
时无法发送;
然后我读到了 jsonp
,现在我不知道如何编写代码;
不知何故,我需要用 jsonp
news 函数中的代码> call;
< script>
功能新闻(数据){
$ .ajax({
'url':'http://domain.com/index.php/api/news',
' data':{'data':data},
'success':function(data){
},
dataType:'json'
});
}
函数唯一(数据){
$ .ajax({
'url':'http://localhost/fb-group/index.php/api/unique ',
'数据':{'数据':数据},
'成功':函数(数据){
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,
'数据':'',
'成功':函数(数据){
if(data&& data.feed&& data.feed.data) {
allData = data.feed.data;
$ .each(allData,function(index,value){
unique(value);
});
}
},
dataType:'json'
});
< / script>
服务器端可以这样做吗?
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->消息:'未知';
$ model_feedM-> data_picture = isset($ data_decoded->图片)? $ data_decoded->图片:'未知';
$ model_feedM-> data_link = isset($ data_decoded-> link)? $ data_decoded->链接:'未知';
$ 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}';
退出;
}
<脚本>
功能新闻(数据){
$ .ajax({
'url':'http://domain.com/index.php/api/news?callback =?',
'数据':{'数据':数据},
'成功':函数(数据){
},
dataType:'json'
});
}
函数唯一(数据){
$ .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 =?',
'数据':'',
'成功':函数(数据){
if(data&& data.feed&& ; data.feed.data){
allData = data.feed.data;
$ .each(allData,function(index,value){
unique(value);
});
}
},
dataType:'json'
});
服务器获取'回调'参数并返回数据包含'callback'参数值,
这是jquery的跨域名!
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;
}
<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!
这篇关于如何使用JSONP从localhost向domain.com发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!