Get json from the web server and process it

Asked 2 years ago, Updated 2 years ago, 43 views

I asked the server php for the value and got the value as json I want to put that list on the list I don't know how to get json. I don't know how to find it, so I'm asking for your help. http://ip/file name.I think I need to make a request to PHP with an address like this I don't know how to request it

html php json

2022-09-21 10:52

3 Answers

The task you want to do consists of a total of 3 steps.

Level 1 is... Let's do a simple impersonation. Make any php file and save it like this.

<?php

echo 'var jason = '.json_encode([
    'data' => [
        ['foo' => 'bar', 'lorem' => 1],
        ['foo' => 'dee', 'lorem' => 2]
    ]
]).';';

And write HTML roughly like this.

<html>
<body>
    <div id="result"></div>
    <script src="The php file you created earlier URL.php"></script><-- This must come out first. Just importing jason variables directly into PHP text body -->
    <script>
    if (jason.length && jason.data) {
        for (i = 0; i < jason.data.length; i++) {
            var onedata = jason.data[i];
            document.getElementById('result').innerHTML += '<p>foo = ' + onedata.foo + ', lorem = ' + onedata.lorem + '</p>';
        }
    }
    </script>
</body>
</html>

Then you'll get something similar to demo. In fact, whether it's PHP or Java, you only need to create a valid format string that can be put in JSON.parse() in JavaScript. Making a list is a very simple task at the end.

Try it!


2022-09-21 10:52

<!Doctype html>
<head>
<script>

function httpGet(theUrl) {
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} } else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        return xmlhttp.responseText;
    } } else {
        return null;
    }
}
xmlhttp.open("GET", theUrl, false);
xmlhttp.send();    
}

varphp = httpGet ("http://ip address/test.php"));
var json = JSON.parse(php);

// -- Up to here, step 2 and up to here, step 3

if (json.data) {
    for (i = 0; i < php.data.length; i++) {
        var onedata = php.data[i];
        document.getElementById('result').innerHTML += '<p>foo = ' + o        nedata.foo + ', lorem = ' + onedata.lorem + '</p>';
    }
}
</script>

</head>

I turned it like this, but why does it only show a white screen?ㅠ<


2022-09-21 10:52

 var php = '{"data":[{"foo":"bar","lorem":1},{"foo":"dee","lorem":2}]}';

Json parsing works normally when php is declared like this Use httpget to get phpurl http:// ip/test.php I can't get Json because I put it in like this, but it's blocked again crying If you write http:// ip/test.php in the address bar, {"data":[{"foo":"bar","lorem":1},{"foo":"dee","lorem":2}]}'; This data comes out the same.ㅠ<


2022-09-21 10:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.