find out why i get a Price: undefined CHF on this script: <!DOCTYPE html> <html> <head>…
- User
find out why i get a Price: undefined CHF on this script:
code$( "#to" ).autocomplete({ source: function( request, response ) { $.ajax({ url: "https://timetable.search.ch/api/completion.en.json", dataType: "jsonp", data: { term: request.term }, success: function( data ) { response( data ); } }); }, minLength: 2 }); $("#calculate").on("click", function() { var from = $("#from").val(); var to = $("#to").val(); $.ajax({ url: "https://timetable.search.ch/api/route.en.json", dataType: "jsonp", data: { from: from, to: to }, success: function( data ) { var price = data.price; $("#result").text("Price: " + price + " CHF"); }, error: function() { alert("An error occurred. Please try again later."); } }); }); }); - Assistant
The reason you get "Price: undefined CHF" is because the API response doesn't have a field called 'price'. In order to see the actual response, you can log it to the console.
Modify the success function inside the $("#calculate").on("click", function() {}) as below:
codesuccess: function( data ) { console.log(data); // Add this line to log the AJAX response var price = data.price; $("#result").text("Price: " + price + " CHF"); },Now, open the HTML page in your browser and check the console (press F12 and go to "Console" tab) to see the logged data when you click on "Calculate Price". You will see that there is no field called 'price' in the response.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.