json - how to print this i m not getting any output train api
问题描述
This is my js page
$(document).ready(function(){
$("#submitCity").click(function(){
return getWeather();
});
});
function getWeather(){
var city = $("#city").val();
if(city != ''){
$.ajax({
type: "GET",
dataType: "jsonp",
success: function(data){
var widget = showResults(data);
$("#showWeather").html(widget);
$("#city").val('');
}
});
}else{ }
}
function showResults(data){
return "<h3 style='padding-left:40px; padding-bottom:30px;'><strong>Wind Direction</strong>: "+data+"°</h3>";
}
the url works correctly on chrome
"reservation_upto": {"name": "RATLAM JN", "lng": 75.166667, "lat": 23.583333, "code": "RTM"}, "to_station": {"name": "RATLAM JN", "lng": 75.166667, "lat": 23.583333, "code": "RTM"}, "passengers": [{"current_status": "CNF/-/0/GN", "booking_status": "CNF/B2/45/GN", "no": 1}], "boarding_point": {"name": "LUCKNOW", "lng": 80.9346001, "lat": 26.8381, "code": "LKO"}, "chart_prepared": false, "debit": 3, "journey_class": {"name": null, "code": "3A"}, "train": {"name": "SABARMATI EXP", "classes": [{"name": "3rd AC ECONOMY", "available": "N", "code": "3E"}, {"name": "SECOND SEATING", "available": "N", "code": "2S"}, {"name": "SECOND AC", "available": "N", "code": "2A"}, {"name": "THIRD AC", "available": "Y", "code": "3A"}, {"name": "FIRST AC", "available": "N", "code": "1A"}, {"name": "FIRST CLASS", "available": "N", "code": "FC"}, {"name": "AC CHAIR CAR", "available": "N", "code": "CC"}, {"name": "SLEEPER CLASS", "available": "Y", "code": "SL"}], "number": "19166", "days": [{"runs": "Y", "code": "MON"}, {"runs": "N", "code": "TUE"}, {"runs": "Y", "code": "WED"}, {"runs": "N", "code": "THU"}, {"runs": "N", "code": "FRI"}, {"runs": "Y", "code": "SAT"}, {"runs": "N", "code": "SUN"}]}, "pnr": "6617656248", "from_station": {"name": "SONPUR JN", "lng": 82.5947818084139, "lat": 27.88121275, "code": "SEE"}}
解决方案
datatype jsonp is not supported by your api, use json or nothing, will work, and then loop through the data,
you are getting array with arrays in json , so nested loop will be needed
$(document).ready(function(){
function getWeather(){
var city = $("#city").val();
if(city != ''){
$.ajax({
url: "api",
type: "GET",
dataType: "json",// or don't use datatype, just comment it out,
success: function(data){
console.log(data);
var phtml='';
$.each( data, function( key, value ) {
phtml = phtml + key +' = '+ value;
})
var widget = "<h3 style='padding-left:40px; padding-bottom:30px;'><strong>Wind Direction</strong>: "+phtml+"°</h3>";
$("#showWeather").html(widget);
$("#city").val('');
}
});
}
}
推荐阅读
- typescript - 条件打字稿 - 转换嵌套对象/数组中的值
- python - Python返回'UFuncTypeError:ufunc'add'不包含循环..'
- c# - 从 ASP.NET Core 上传多个文件并存储到数据库中
- javascript - Vue js 只允许输入 2 位小数而无需四舍五入
- ios - 多个日期选择器 - 使用已更改的值
- javascript - Jquery Anchor Tag 不打开一个弹出窗口
- coveo - 在进行智能发布时,Sitecore 项目未在 Coveo 中建立索引
- arduino - Arduino Esp8266 LittleFS 上传更新
- python - Kivy:如何填充嵌套的回收视图,它位于 Kivy 的弹出窗口中?
- python-3.x - 如何一次删除所有角色(Discord.py 1.4.1)