Get JSON in Javascript (via URI or PHP)?

0

At this point I find myself working on a project, and I need to call a webservice that should resume a json, later it will be interpreted and fill a dropdown list. In fact, part of the code I need for this work, I already found it, in an article here on the web, but there is a problem. JSON is stored in a local variable, and I need to get the same from a REST webservice.

<script type="text/javascript">


        var myjson;
        $.getJSON("http://localhost:8080/revistaSystem/resplaneamento/ServicoComposicao/ObterTodasAsComposicoes/", function(json){
            myjson = json;
            console.log(json);
        });

        function PopulateDropDownList() {
           //Build an array containing Customer records.
            /*
            var customers = [
                { CustomerId: 1, Name: "John Hammond", Country: "United States" },
                { CustomerId: 2, Name: "Mudassar Khan", Country: "India" },
                { CustomerId: 3, Name: "Suzanne Mathews", Country: "France" },
                { CustomerId: 4, Name: "Robert Schidner", Country: "Russia" }
            ];*/

            var customers = myjson;

            var ddlCustomers = document.getElementById("ddlCustomers");

            //Add the Options to the DropDownList.
            for (var i = 0; i < customers.length; i++) {
                var option = document.createElement("OPTION");

                //Set Customer Name in Text part.
                option.innerHTML = customers[i].Name;

                //Set CustomerId in Value part.
                option.value = customers[i].CustomerId;

                //Add the Option element to DropDownList.
                ddlCustomers.options.add(option);
            }
        }
    </script>

I want to get json and render the same before the page is completely loaded, so I opted for the html page:

<body onload="PopulateDropDownList()">
</body>

As you can see above I intend to load JSON, and I made some attempts that failed. As I work with php, in this project I then also tried to get JSON from PHP, to later put it on the side of javascript. I had enough difficulties.

  <?php
    $uri ="http://localhost:8080/revistaSystem/resplaneamento/ServicoComposicao/ObterTodasAsComposicoes/";
    $json = file_get_contents($uri);
    $obj = json_decode($json, true);
    echo json_encode($obj);

  ?>

Summary: I need to get the JSON, from a URI, and after getting this, populate a dropdown list, with values. This whole process has to be performed before loading the page. Just out of curiosity, making the call to my REST webservice, it returns the JSON in the following format:

[{"numPK":1,"composicao":"100% Algodao"},{"numPK":2,"composicao":"40% Linho/60% Algodao"},{"numPK":3,"composicao":"30% Linho/70% Algodao"}] 

Can you give me a concrete example. Thank you!

    
asked by anonymous 30.03.2018 / 00:33

1 answer

0

You can force it to wait for one process to finish initializing another with  async: true or false.

exemplo
 url: url,
 mtype: 'GET',
 datatype: 'json',
 async: false,
    
03.04.2018 / 21:48