php - Call a website and grab text using javascript(api?) -


this question has answer here:

i trying use website: https://api.mojang.com/users/profiles/minecraft/[variable_here] (use https://api.mojang.com/users/profiles/minecraft/minecraftiscewl example)

i want change variable based on user input, , grab text page:

{"id":"36c84ed1708a4fc1b31e031bf1511de6","name":"[text_here]"}

can javascript(and how), or need use php?

the answer @ how make ajax call without jquery? shows how this.

basically make xmlhttprequest url, expecting application/json accept type. pull js using json.parse convert incoming data js object. can interact normally.

<script type="text/javascript"> function loadxmldoc() { var xmlhttp;  if (window.xmlhttprequest) {     // code ie7+, firefox, chrome, opera, safari     xmlhttp = new xmlhttprequest(); } else {     // code ie6, ie5     xmlhttp = new activexobject("microsoft.xmlhttp"); }  xmlhttp.onreadystatechange = function() {     if (xmlhttp.readystate == xmlhttprequest.done ) {        if(xmlhttp.status == 200){            document.getelementbyid("mydiv").innerhtml = xmlhttp.responsetext;        }        else if(xmlhttp.status == 400) {           alert('there error 400')        }        else {            alert('something else other 200 returned')        }     } }  xmlhttp.open("get", "https://api.mojang.com/users/profiles/minecraft/[variable_here]", true); xmlhttp.send(); 

}

there's number of ways of interacting xmlhttprequest object (see: https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest). alternatively, can use jquery stated in answer if have loaded other purposes.

the object resulting json.parse() call json posted, like:

var playerdata = json.parse(datafromajaxcall); console.log(playerdata.name); //[text_here] 

Comments