Before anything else, I must acknowledge the cross-domain issues that may arise with JavaScript. The fact that I am seeing both 404 and 200 response codes in my console has made me reconsider this possibility.
The scenario... I am currently developing a website prototype featuring a video player that has a variety of file options. I am trying to establish a logic that goes as follows: if this file is available, play it if it's not available, try another file if that file is available, play it if it's not available, try another file and so on...
Initially, I attempted to accomplish this using XMLHttpRequest(). However, I encountered difficulty in implementing if/else logic due to the need to specify the URL in .open(). Upon discovering jQuery's .ajax() function with error/success options, I saw a potential solution. It occurred to me that I could nest .ajax() within the error function. As a result, progress has been made but not without encountering Access-Control-Allow-Origin errors alongside valid 404/200 responses. Hence, my question is "How can I incorporate those 404/200 responses into my code?"
Regarding JavaScript (and please suggest a more effective approach)...
function getSermonVideo() {
"use strict";
const params = new URLSearchParams(document.location.search);
const filename = params.get("filename");
const site = params.get("site");
let date = params.get("date");
let shortdate = date.slice(2);
shortdate = shortdate.replaceAll("-", "");
const editedvideourl = "https://flcmedia.org/productvideo/";
const editedaudiourl = "https://flcmedia.org/product/";
let uneditedurl = "https://flcmedia.org/" + site + "/";
jQuery.ajax({
url:editedvideourl + filename + ".mp4",
type:'HEAD',
error: function() {
jQuery.ajax({
url:editedvideourl + filename + "_480.mp4",
type:'HEAD',
error: function() {
jQuery.ajax({
url:editedaudiourl + filename + ".mp3",
type:'HEAD',
error: function() {
jQuery.ajax({
url: uneditedurl + "MP4/" + date.slice(0, 4) + "/" + shortdate + "Video.mp4",
type:'HEAD',
error: function() {
jQuery.ajax({
url: uneditedurl + "MP3/" + date.slice(0, 4) + "/" + shortdate + "Audio.mp3",
type:'HEAD',
error: function() {
//console.log("No file exists.");
},
success: function() {
videoplayer.src = uneditedurl + "MP3/" + date.slice(0, 4) + "/" + shortdate + "Audio.mp3"
}
});
},
success: function() {
videoplayer.src = uneditedurl + "MP4/" + date.slice(0, 4) + "/" + shortdate + "Video.mp4"
}
});
},
success: function() {
videoplayer.src = editedaudiourl + filename + ".mp3";
}
});
},
success: function() {
videoplayer.src = editedvideourl + filename + "_480.mp4";
}
});
},
success: function() {
videoplayer.src = editedvideourl + filename + ".mp4";
}
});
}
Edge Developer's Tools - Console...
Access to XMLHttpRequest at 'https://flcmedia.org/productvideo/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine.mp4' from origin 'https://anti-exe.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
HEAD https://flcmedia.org/productvideo/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine.mp4 net::ERR_FAILED 200 (OK)
Access to XMLHttpRequest at 'https://flcmedia.org/productvideo/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine_480.mp4' from origin 'https://anti-exe.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
HEAD https://flcmedia.org/productvideo/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine_480.mp4 net::ERR_FAILED 404 (Not Found)
Access to XMLHttpRequest at 'https://flcmedia.org/product/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine.mp3' from origin 'https://anti-exe.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
HEAD https://flcmedia.org/product/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine.mp3 net::ERR_FAILED 200 (OK)
Access to XMLHttpRequest at 'https://flcmedia.org/FLCSarasota/MP4/2017/171126Video.mp4' from origin 'https://anti-exe.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
HEAD https://flcmedia.org/FLCSarasota/MP4/2017/171126Video.mp4 net::ERR_FAILED 200 (OK)
Access to XMLHttpRequest at 'https://flcmedia.org/FLCSarasota/MP3/2017/171126Audio.mp3' from origin 'https://anti-exe.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
HEAD https://flcmedia.org/FLCSarasota/MP3/2017/171126Audio.mp3 net::ERR_FAILED 200 (OK)
Access to fetch at 'https://flcmedia.org/productvideo/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine.mp4' from origin 'https://anti-exe.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
GET https://flcmedia.org/productvideo/1214-GodsIncorruptibleWordSeed-01-HisWordIsMedicine.mp4 net::ERR_FAILED 200 (OK)
As evident from the responses, some files return 200 while others return 404. Utilizing this information will enable me to implement the desired if/else logic. Do you have any suggestions?