KINNARI
KINNARI

Reputation: 181

multiple key and value from JSON world markets api for ajax

I have created API real time API for World Stock Markets, includes Nifty, Dow Jones, Nasdaq, SGX Nifty etc.

Real Time API: http://www.yourtradelog.com/liveapi/world-markets

From This JSON data, I have created ajax and css for SENSEX with ajax code. Now My question is, How can I use multiple in single ajax. Suppose, I want to created same thing and out for NIFTY fron this json data, how is possible in this my code:

AJAX code:

 $.ajax({
        type:"get",
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        url:"{{url('liveapi/world-markets')}}",
        data:{"_token": "{{ csrf_token() }}"},
        dataType:'json',
        success:function(res){
            if(!res){
                alert('Something went wrong')
            }else{

                var output = document.getElementById("chg"),
                    oldValue = parseInt(output.dataset.oldValue || "0");

                output.className = "";
                output.innerHTML = res.sensex.lastprice;
                output.dataset.oldValue = res.sensex.lastprice;
                console.log(res);
              //  output.classList.add(res.nasdaq.lastprice > 0 ? "greenText" : "redText");
                if (res.sensex.lastprice > oldValue) {
                    output.classList.add("greenText");
                } else {
                    output.classList.add("redText");
                }

                $('#live_change').text("("+res.sensex.changed+")" );
                $('#per_change').html('<b>'+res.sensex.perchange+'%</b>');


                if(res.sensex.changed > 0) {

                    document.getElementById("live_change").className = "green";
                    document.getElementById("per_change").className = "green";
                } else {
                    document.getElementById("live_change").className = "red";
                    document.getElementById("per_change").className = "red";

                }
            }
        }
    });
},1000);

Above code's out put is:

enter image description here

Upvotes: 0

Views: 111

Answers (1)

Arun Kumar
Arun Kumar

Reputation: 885

You can use the same ajax call as you are getting all data in single ajax call in success.

$.ajax({
        type:"get",
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        url:"{{url('liveapi/world-markets')}}",
        data:{"_token": "{{ csrf_token() }}"},
        dataType:'json',
        success:function(res){
            if(!res){
                alert('Something went wrong')
            }else{

                var output = document.getElementById("chg"),
                    oldValue = parseInt(output.dataset.oldValue || "0");

                output.className = "";
                output.innerHTML = res.sensex.lastprice;
                output.dataset.oldValue = res.sensex.lastprice;
                console.log(res);
              //  output.classList.add(res.nasdaq.lastprice > 0 ? "greenText" : "redText");
                if (res.sensex.lastprice > oldValue) {
                    output.classList.add("greenText");
                } else {
                    output.classList.add("redText");
                }

                $('#live_change').text("("+res.sensex.changed+")" );
                $('#per_change').html('<b>'+res.sensex.perchange+'%</b>');


                if(res.sensex.changed > 0) {

                    document.getElementById("live_change").className = "green";
                    document.getElementById("per_change").className = "green";
                } else {
                    document.getElementById("live_change").className = "red";
                    document.getElementById("per_change").className = "red";

                }
                // CODE FOR nifty_50 
                var nifty_lastPrice=res.nifty_50.lastprice;
                var nifty_changed=res.nifty_50.changed;
                //

                // CODE FOR nifty_50 
                var nasdaq_lastPrice=res.nasdaq.lastprice;
                var nasdaq_changed=res.nasdaq.changed;
                //
                // CODE FOR dow_jones 
                var dow_jones_lastPrice=res.dow_jones.lastprice;
                var dow_jones_changed=res.dow_jones.changed;
                //

            }
        }
    });
},1000);

Upvotes: 1

Related Questions