-3

I have A json file at a remote URL as follows.

http://example.com/stats?json=1&callback=serverInfo

I need to have:

"songtitle":"Live with the music station on the internet"

From the remote part.

I have a javascript file for songtitle. So i need the remote songtitle on:

var str = remotesongtitle;

Who can help me with this?

Many thanx!

Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
HermanF
  • 5
  • 4
  • 1
    could you please explain what are you looking for more clearly. If you are trying to access property songtitle from json just use obj.songtitle – user93 May 01 '18 at 15:49
  • You need to detail out the structure of the response json you have after the callback. Your javascript assignment would depend on that. From what you've described so far, it's just unclear. – Sudheesh Singanamalla May 01 '18 at 15:51
  • On a remote site is a json array. I need to have the songtitle from this array in my local script on other website. http://example.com/stats?json=1&callback=serverInfo These link, and i need the songtitle from the array. I need that songtitle in my var str = – HermanF May 01 '18 at 15:51
  • Can you give us a dummy object structure? Does it look like `[ { "songtitle" : "some random content" }, { .... }, { ....} ]`? – Sudheesh Singanamalla May 01 '18 at 15:56
  • http://streamunit.nl:9173/stats?json=1&callback=serverInfo – HermanF May 01 '18 at 15:58
  • So i do include that page? And: var str = serverInfo['songtitle']; ?? – HermanF May 01 '18 at 15:58
  • is `serverInfo` the parameter to the callback function `serverInfo()`? Did you declare it as `function serverInfo(serverInfo) { } `? – Sudheesh Singanamalla May 01 '18 at 16:02
  • serverInfo is the parameter of callback function indeed. I dont declare it i need the songtitle in in a local javascipt. – HermanF May 01 '18 at 16:03
  • I need to include the JSON array in my html file to get the songtitle in the var str. – HermanF May 01 '18 at 16:04
  • This is not a json array. it's just an individual `json` object. Please see my answer below for a working sample – Sudheesh Singanamalla May 01 '18 at 16:07

2 Answers2

0

Here's a sample implementation:

function serverInfo(res) {
    var str = res.songtitle;
    console.log(str);
}

// Create a request and use serverInfo() callback
var script = document.createElement('SCRIPT');
script.type = 'text/javascript';
script.src = 'http://streamunit.nl:9173/stats?json=1&callback=serverInfo';
document.body.appendChild(script);

The output of the console shows:

Monica west - Die oude cabriolet
Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
  • Many thanx! How i use str outside the function { } ? – HermanF May 01 '18 at 16:20
  • What do you mean by outside the function? You can just have the var str as a variable accessible throughout the program by changing its scope from the local scope of the function to [the global scope](https://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function). – Sudheesh Singanamalla May 01 '18 at 16:28
  • See my reply on the question. – HermanF May 01 '18 at 16:36
  • You are using the `switch()` outside the function, Put that switch inside the `function serverInfo()`. Please read more about global variables in javascript. What you're asking for is trivial – Sudheesh Singanamalla May 01 '18 at 16:38
0

This should work.

P.S. : The code below will not work here, as the browser will not allow secure-less request(http) on secured website(https) and cancels at source.

var req = new XMLHttpRequest();
req.open("GET","http://streamunit.nl:9173/stats?json=1",true);
req.onreadystatechange=function(){
    if(req.readyState === 4)
    {
        if(req.status === 200)
        {
            console.log(JSON.parse(req.responseText).songtitle);
        }
    }
};
req.send();
Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42