1

I have a url contain all the json format.

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. Are there any way that i can retrieved those json text

2
  • 1
    I can not see any JSON in your code Commented Dec 14, 2011 at 15:45
  • 1
    Formatting code properly helps other people to understand your problem. Your problem is: You are calling readXML directly and assign its return value to onreadystatechange instead of the function itself. Also note that you cannot simply make requests to 3rd party domains. You'd have to make use of JSONP, which the server has to support. Commented Dec 14, 2011 at 15:45

2 Answers 2

2

The problem is with req.onreadystatechange = readXML();. You're assigning the result of the function instead of the function itself (as a callback).

You want 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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. 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); } 

1 Comment

How can I set Access-Control-Allow-Origin?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.