跨域ajax响应失败 [英] Cross domain ajax response failing
问题描述
好吧...因为这个鳕鱼片在这段时间里已经扭曲了一段时间了...
首先我用这个代码工作...
$(document).ready(function(){
$('#content')。html('');
$ .ajax({
url:'data.json',
dataType:json,
success:function(data){
$ ').append(''+ data.rank +'
');
}
});});
在此代码中,data.json包含此格式的JSON数据:
{
user_id:3190399,
user_name:Anand_Dasgupta,
followers_current:86,
date_updated:2009-06-04,
url:,
avatar:205659924 / DSC09920_normal.JPG
follow_days:0,started_followers:86,
growth_since:0,
average_growth:0,
tomorrow: 86,
next_month:86,
followers_yesterday:86,
rank:176184,
followers_2w_ago:null,
growth_since_2w:86,
average_growth_2w:6,
tomorrow_2w:92,
next_month_2w:266,
followerserdate :[]
}
此数据来自URL:
http://twittercounter.com/api/?username=Anand_Dasgupta&输出= json& results = 3
(点击URL获取数据)
但是当我在$中替换data.json。 ajax函数的URL包含相同的数据,下面的代码似乎不工作...
$(document).ready function(){
$('#content')。html('');
$ .ajax({
url:' http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3 ',
dataType:json,
success:function(data){
$('#content')。append(''+ data.rank +'
');
}
});});
我早点把这个问题提出来StackOverflow和响应是,这是一个十字域问题。
所以我阅读关于跨域ajax请求,这是我想出的代码:
$(document).ready(function(){
$('form#search')。bind
e.preventDefault();
$('#content')。html('');
//定义回调函数
function get(jsonData ){
$('#content')。append(''+ jsonData.rank +'
');
bObj.removeScriptTag();
}
//网络服务调用
var req ='http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=get';
//创建一个新的请求对象
bObj = new JSONscriptRequest(req);
//构建动态脚本标签
bObj.buildScriptTag();
//将脚本标记添加到页面
bObj.addScriptTag();
});
});
但是即使似乎无效。
如果任何人可以提供任何帮助,那么它将深深赞赏。
我给了整个代码,以便任何人都可以自己测试它们。
谢谢
Anand
您必须查看相关网站是否支持 JSONP 。
您还必须使用 dataType 选项在ajax调用设置为'jsonp'
,以及指定正确的回调
Ok...so ive been wriggling around with this piece of cod for quite a while now...
first i used this code which worked...
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'data.json',
dataType: "json",
success: function(data) {
$('#content').append(''+data.rank+'
');
}
});});
In this code (it works) data.json contains the JSON data in this format:
{ "user_id":"3190399", "user_name":"Anand_Dasgupta", "followers_current":"86", "date_updated":"2009-06-04", "url":"", "avatar":"205659924/DSC09920_normal.JPG", "follow_days":"0","started_followers":"86", "growth_since":0, "average_growth":"0", "tomorrow":"86", "next_month":"86", "followers_yesterday":"86", "rank":176184, "followers_2w_ago":null, "growth_since_2w":86, "average_growth_2w":"6", "tomorrow_2w":"92", "next_month_2w":"266", "followersperdate":[] }
This data comes from the URL:
http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3 (Click the URL to get the data)
But when I replace data.json in the $.ajax function with the URL which contains the same data, this code below doesn't seem to work...
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3',
dataType: "json",
success: function(data) {
$('#content').append(''+data.rank+'
');
}
});});
I had earlier put this question forth on StackOverflow and responses were that this is a cross domain problem.
So i read about cross domain ajax request and this is the code that i came up with:
$(document).ready(function() {
$('form#search').bind("submit", function(e){
e.preventDefault();
$('#content').html('');
// Define the callback function
function get(jsonData) {
$('#content').append(''+jsonData.rank+'
');
bObj.removeScriptTag();
}
// The web service call
var req = 'http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=get';
// Create a new request object
bObj = new JSONscriptRequest(req);
// Build the dynamic script tag
bObj.buildScriptTag();
// Add the script tag to the page
bObj.addScriptTag();
});
});
But even doesnt seem to be working.
If anyone can provide any help then it will be deeply appreciated. I have given the entire code so that anyone could test it themselves if they want to.
Thank you Anand
You have to see if the site in question supports JSONP.
You have to also use the dataType option in your ajax call set to 'jsonp'
, as well as specify the correct callback argument.
这篇关于跨域ajax响应失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!