首页 > 解决方案 > 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+"&deg;</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"}}

标签: jsonajaxapi

解决方案


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+"&deg;</h3>";

             $("#showWeather").html(widget);

            $("#city").val('');
        }

    });


}


}

推荐阅读