I have an AJAX call (try to disregard all of the serialization, that part works 100%) that's throwing a 304. How would I set this up so my data (flightResults) doesn't get overwritten by the empty 304 response body? Code below...apologies for the long snippet, just want to get it all in there..Thanks!
var app = {};
app.init = function(){
app.flightsGeo();
};
app.flightsGeo = function(){
var location = resp.headers.get('Location');
var sessionKey = location.substring(location.lastIndexOf("/") + 1);
let updatedURL = `https://skyscanner-skyscanner-flight-search-v1.p.mashape.com/apiservices/pricing/uk2/v1.0/${sessionKey}?sortType=price&sortOrder=asc&pageIndex=0&pageSize=5`
$.ajax({
type: "GET",
url: updatedURL,
dataType: 'json',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Mashape-Key': 'yOLGSU9nWcmshQAjA5nsXiBvEooWp1v2Q9Jjsncjrhz58ajvZM',
'X-Mashape-Host': 'skyscanner-skyscanner-flight-search-v1.p.mashape.com',
},
success: function(response){
},
error: function(error){
self.set('loading', false);
self.set('searchError', true);
}
}).done(function(response){
var itineraries = response.Itineraries;
var agents = response.Agents;
var legs = response.Legs;
var carriers = response.Carriers;
var places = response.Places;
$.each(itineraries, function(itineraryKey, itineraryVal){
var inbound = app.getObjects( legs, 'Id', itineraryVal.InboundLegId),
inDepartureTime = inbound[0].Departure,
inArrivalTime = inbound[0].Arrival,
inDepartureDate = inbound[0].Departure,
inArrivalDate = inbound[0].Arrival,
inOrigin = app.getObjects( places, 'Id', inbound[0].DestinationStation),
inDestination = app.getObjects( places, 'Id', inbound[0].OriginStation),
inCarriers = app.getObjects( carriers, 'Id', inbound[0].OperatingCarriers[0]);
var outbound = app.getObjects( legs, 'Id', itineraryVal.OutboundLegId),
outDepartureTime = outbound[0].Departure,
outArrivalTime = outbound[0].Arrival,
outDepartureDate = outbound[0].Departure,
outArrivalDate = outbound[0].Arrival,
outOrigin = app.getObjects( places, 'Id', outbound[0].DestinationStation),
outDestination = app.getObjects( places, 'Id', outbound[0].OriginStation),
outCarriers = app.getObjects( carriers, 'Id', outbound[0].OperatingCarriers[0]);
var agent = app.getObjects( agents, 'Id', itineraryVal.PricingOptions[0].Agents[0]);
var price = itineraryVal.PricingOptions[0].Price.toFixed(2);
var permalink = itineraryVal.PricingOptions[0].DeeplinkUrl;
var time = itineraryVal.PricingOptions[0].QuoteAgeInMinutes;
var data = {
agent: agent,
price: price,
time : time,
permalink: permalink,
inbound: {
time: {
departure: inDepartureTime,
arrival: inArrivalTime
},
date: {
departure: inDepartureDate,
arrival: inArrivalDate
},
station: {
origin: inOrigin,
destination: inDestination
},
carriers: inCarriers
},
outbound: {
time: {
departure: outDepartureTime,
arrival: outArrivalTime
},
date: {
departure: outDepartureDate,
arrival: outArrivalDate
},
station: {
origin: outOrigin,
destination: outDestination
},
carriers: outCarriers
}
};
results.push(data);
});
self.set('flightResults', results);
self.set('loading', false);
self.set('complete', true);
}).fail(function(){
alert("Something went wrong");
});
};
app.getObjects = function(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(app.getObjects(obj[i], key, val));
} else
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
};
var results = [];
$(document).ready(app.init);
return app;