我为世界股票市场创建了API real time API
,其中包括 Nifty,道琼斯,纳斯达克,新交所Nifty 等。
实时API:http://www.yourtradelog.com/liveapi/world-markets
根据此JSON数据,我已使用Ajax代码为SENSEX创建了ajax和css。现在我的问题是,如何在单个ajax
中使用多个。假设我想创建同样的东西,然后从json数据中获取NIFTY,这在我的代码中怎么可能:
AJAX代码:
$.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);
以上代码的输出为:
答案 0 :(得分:1)
您可以使用同一ajax调用,因为您将成功在单个ajax调用中获取所有数据。
$.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);