简体   繁体   English

使用jQuery $ .getJSON的JSONP请求不适用于格式正确的JSON

[英]JSONP request using jquery $.getJSON not working on well formed JSON

I'm not sure is it possible now from the url I am trying. 我不确定现在是否可以从我尝试的网址中获取。 Please see this url: http://www.heiaheia.com/voimakaksikko/stats.json 请参阅以下网址: http : //www.heiaheia.com/voimakaksikko/stats.json

It always serves the same padding function "voimakaksikkoStats". 它始终提供相同的填充功能“ voimakaksikkoStats”。 It is well formed JSON, but I have not been able to load it from remote server. 它是格式正确的JSON,但我无法从远程服务器加载它。 Does it need some work from the server side or can it be loaded with javascript? 它需要从服务器端进行一些工作还是可以用javascript加载? I think the problems gotta to have something to with that callback function... 我认为问题必须与该回调函数有关...

JQuery is not requirement, but it would be nice. JQuery不是必需的,但是会很好。

This (callback=voimakaksikkoStats) returns nothing (firebug -> net -> response), and alert doesn't fire: 此(callback = voimakaksikkoStats)不返回任何内容(firebug-> net-> response),并且警报不会触发:

$.getJSON("http://www.heiaheia.com/voimakaksikko/stats.json?callback=voimakaksikkoStats", function(data){
    alert(data);
})

but this (callback=?): 但这(回调=?):

 $.getJSON("http://www.heiaheia.com/voimakaksikko/stats.json?callback=?", function(data){
    alert(data);
})

returns: 返回:

voimakaksikkoStats({"Top5Sports":[],"Top5Tests":{"8":"No-exercise ennuste","1":"Painoindeksi","2":"Vy\u00f6t\u00e4r\u00f6n ymp\u00e4rys","10":"Cooperin testi","4":"Etunojapunnerrus"},"Top5CitiesByTests":[],"Top5CitiesByExercises":[],"ExercisesLogged":0,"Top5CitiesByUsers":[""],"TestsTaken":22,"RegisteredUsers":1});

But I cannot access it... In both examples the alert never fires. 但是我无法访问它。在这两个示例中,警报都不会触发。 Can someone help? 有人可以帮忙吗?

<script type="text/javascript">
function voimakaksikkoStats(stats) {
  var ul = new Element('ul');
  ul.insert(new Element('li').update('Registered users: '+ stats['RegisteredUsers']));
  ul.insert(new Element('li').update('Tests taken: '+ stats['TestsTaken']));
  ul.insert(new Element('li').update('Top5 sports: '+ stats['Top5Sports'].join(', ')));
  $(document.body).insert({'bottom': ul});
}
</script>
<script type="text/javascript" src="http:/www.heiaheia.com/voimakaksikko/stats.json"></script>

This example uses Prototype.js to create list with some data from given stats, and then puts this list at the bottom of document body. 本示例使用Prototype.js从给定的统计信息中创建带有一些数据的列表,然后将该列表放在文档主体的底部。

is the script trying to fetch json from http://www.heiaheia.com also on http://www.heiaheia.com ? 是脚本试图获取从JSON http://www.heiaheia.comhttp://www.heiaheia.com

if not this is the cause, it's currently not authorized to make request (using javascript) to another server than the one serving the script 如果不是这个原因,则当前无权向使用脚本的服务器以外的其他服务器发出请求(使用javascript)

为了使您的测试功能正常工作,请尝试更改为callback=?

If the your request goes to anonther domain try using jsonP method. 如果您的请求转到另一个域,请尝试使用jsonP方法。 Search for jsonP docs 搜索jsonP文档

<script type="text/javascript">
function voimakaksikkoStats(obj) {
    alert(obj.TestsTaken);
}
</script>
<script type="text/javascript" src="http://www.heiaheia.com/voimakaksikko/stats.json"></script>

I never got it working with jQuery, but the simple code above solved my problems. 我从来没有使用过jQuery,但是上面的简单代码解决了我的问题。 I found help from Yahoo: http://developer.yahoo.com/common/json.html 我从Yahoo找到了帮助: http : //developer.yahoo.com/common/json.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM