简体   繁体   English

如何从URL解析JSON

[英]How to parse json from a url

I have a url contain all the json format. 我有一个包含所有json格式的url。

http://api.musixmatch.com/ws/1.1/track.lyrics.get?apikey=d34fb59a16877bd1c540aa472491825b&track_id=12414632 http://api.musixmatch.com/ws/1.1/track.lyrics.get?apikey=d34fb59a16877bd1c540aa472491825b&track_id=12414632

function load() { 
    dashcode.setupParts(); 
    var link = 'http://api.musixmatch.com/ws/1.1/track.search?apikey=d34fb59a16877bd1c540aa472491825b&q_track=back%20to%20december&page_size=10'; 
   req.open = ("GET", link, false); 
   req.onreadystatechange = readXML(); 
   req.send(null); 
}

function readXML(){ 
    alert(req.responseText); 
}

this code keep saying null all the time. 这段代码一直都在说null。 Are there any way that i can retrieved those json text 有什么方法可以检索那些json文本

The problem is with req.onreadystatechange = readXML(); 问题出在req.onreadystatechange = readXML(); . You're assigning the result of the function instead of the function itself (as a callback). 您正在分配函数的结果而不是函数本身(作为回调)。

You want req.onreadystatechange = readXML; 你想要req.onreadystatechange = readXML; . Though I must say I'm not sure how this code is supposed to work. 虽然我必须说我不确定这段代码应该如何工作。 Not in terms of how the XHR is made, nor with regards to the external domain. 既不是关于XHR的制造方式,也不是关于外部领域。

Correct usage is as follows.You can check this link http://jsfiddle.net/UH4KY/1/ The link will alert undefined since cross domain scripting is not allowed .You can set the Access-Control-Allow-Origin and test the code. 正确用法如下。您可以查看此链接http://jsfiddle.net/UH4KY/1/链接将提示未定义,因为不允许跨域脚本。您可以设置Access-Control-Allow-Origin并测试码。 function readXML(req) { alert(req); function readXML(req){alert(req); } }

function load() {
    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var link = 'http://api.musixmatch.com/ws/1.1/track.search?apikey=d34fb59a16877bd1c540aa472491825b&q_track=back%20to%20december&page_size=10';

    //req.open = ("GET", link, false);
    xmlhttp.onreadystatechange = function(){ alert(xmlhttp.responseText); }
    xmlhttp.open("GET", link, false);
    xmlhttp.send(null);
}

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

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