这是我的js页面
$(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>";
}
该网址在chrome上正常工作
“ reservation_upto”:{“名称”:“ RATLAM JN”,“ lng”:75.166667,“ lat”: 23.583333,“代码”:“ RTM”},“ to_station”:{“名称”:“ RATLAM JN”,“ lng”:75.166667,“纬度”:23.583333,“代码”:“ RTM”},“乘客”: [{“ 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”,“类别”:[{“名称”:“第三届AC经济”,“可用”: “ N”,“代码”:“ 3E”},{“名称”:“第二个座位”,“可用”:“ N”, “ code”:“ 2S”},{“ name”:“ SECOND AC”,“ available”:“ N”,“ code”:“ 2A”}, {“名称”:“第三交流”,“可用”:“ Y”,“代码”:“ 3A”},{“名称”:“第一 AC“,” available“:” N“,” code“:” 1A“},{” name“:” FIRST CLASS“, “ available”:“ N”,“ code”:“ FC”},{“ name”:“ AC CHAIR CAR”,“ available”: “ N”,“代码”:“ CC”},{“名称”:“ SLEEPER CLASS”,“可用”:“ Y”, “ code”:“ SL”}],“ number”:“ 19166”,“ days”:[{“ runs”:“ Y”,“ code”: “ MON”},{“ runs”:“ N”,“ code”:“ TUE”},{“ runs”:“ Y”,“ code”:“ WED”}, {“运行”:“ N”,“代码”:“ THU”},{“运行”:“ N”,“代码”:“ FRI”},{“运行”: “ Y”,“ code”:“ SAT”},{“ runs”:“ N”,“ code”:“ SUN”}]},“ pnr”: “ 6617656248”,“ from_station”:{“名称”:“ SONPUR JN”,“ lng”: 82.5947818084139,“纬度”:27.88121275,“代码”:“ SEE”}}
答案 0 :(得分:0)
数据类型jsonp,不使用json或不使用jsonp,将可以正常工作,然后遍历数据,
您将在json中获取带有数组的数组,因此将需要嵌套循环
$(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('');
}
});
}
}