无法使用JavaScript访问json文件 [英] Cannot access json file with javascript

查看:120
本文介绍了无法使用JavaScript访问json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读本页面的json内容: http://89.92.40.250:8010/dreamteam/interface/get_event_detail.php?id=397 with javascript 。



所以我开始这个脚本:

  var req = new XMLHttpRequest (); 
req.open(GET,http://89.92.40.250:8010/dreamteam/interface/get_event_detail.php?id=397,true);
req.onreadystatechange = monCode;
req.send(null);

函数monCode(){
if(req.readyState == 4){
var doc = eval('('+ req.responseText +')');
alert(req.responseText);
}
}

当我想显示我使用alert req.responseText);我没有看到内容。另外,我用Firebug语法错误:语法错误:var doc = eval('('+ req.responseText +')');

不可能使用JSONP。 / p>

我们可以提取页面的内容,然后将其转换为JSON格式,然后解析它?

解决方案

如果您无法创建自己的代理,则应该使用YQL来避免跨域问题。

这里是一个示例url for id = 397

  http://query.yahooapis。 COM / V1 /公共/ YQL q =选择%20 *%20from%20json%20where%20url%3D'http%3A%2F%2F89.92.40.250%3A8010%2Fdreamteam%2Finterface%2Fget_event_detail.php%3Fid%3D397 '& format = json& diagnostics = true 

你可以像这样动态设置id, p>

  var id = 397; 
var baseurl =http://query.yahooapis.com/v1/public/yql?q=;
var q =select * from json where url ='http://89.92.40.250:8010/dreamteam/interface/get_event_detail.php?id=\"+id+';
var url = baseurl + encodeURI(q)+& format = json& diagnostics = true;
var req = new XMLHttpRequest();
req.open(GET,url,true);
req.onreadystatechange = monCode;
req.send(null);

函数monCode(){
if(req.readyState == 4){
var data = JSON.parse(req.responseText).query.results;
console.log(data);
//console.log(data.event);
alert(data.event.title);


这是一个jsFiddle DEMO



YQL的使用限制




  • 每个应用程序限制访问密钥):每天100,000个呼叫

  • 每个IP限制:/ v1 / public / :每小时2000个呼叫; / v1 / yql / :每小时20,000个电话


I want to read the json content of this page : http://89.92.40.250:8010/dreamteam/interface/get_event_detail.php?id=397 with javascript.

So i began this script :

var req = new XMLHttpRequest();
    req.open("GET", "http://89.92.40.250:8010/dreamteam/interface/get_event_detail.php?id=397", true); 
    req.onreadystatechange = monCode;   
    req.send(null);      

    function monCode() { 
        if (req.readyState == 4) { 
            var doc = eval('(' + req.responseText + ')');
            alert(req.responseText);
        }
    } 

when I want to show the content I use alert(req.responseText); and I don't see the content. Plus I have a syntax error with Firebug : syntax error : var doc = eval('(' + req.responseText + ')');

Impossible with JSONP.

Can we extract the content of the page, then convert it to JSON format then parse it ??

解决方案

If you can't create a your own proxy, you should use YQL to avoid cross domain problem.

here is an example url for id=397,

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D'http%3A%2F%2F89.92.40.250%3A8010%2Fdreamteam%2Finterface%2Fget_event_detail.php%3Fid%3D397'&format=json&diagnostics=true

and you can set id dynamically like this,

var id = 397;
var baseurl = "http://query.yahooapis.com/v1/public/yql?q=";
var q = "select * from json where url = 'http://89.92.40.250:8010/dreamteam/interface/get_event_detail.php?id="+id+"'";  
var url = baseurl + encodeURI(q) + "&format=json&diagnostics=true";
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.onreadystatechange = monCode;
req.send(null);

function monCode() {
    if (req.readyState == 4) {
        var data = JSON.parse(req.responseText).query.results;
        console.log(data);
        //console.log(data.event);
        alert(data.event.title);
    }
}​

And here is a jsFiddle DEMO

Usage Limits for YQL

  • Per application limit (identified by your Access Key): 100,000 calls per day
  • Per IP limits: /v1/public/: 2,000 calls per hour; /v1/yql/: 20,000 calls per hour

这篇关于无法使用JavaScript访问json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆