我試圖從OMDB API訪問數據,但我在混合內容圖像時遇到問題。 OMDB從IMDB中提取數據,禁止使用其https圖像,因此所有圖像源都必須帶有前綴http。JavaScript混合內容變通辦法
例如,JSON用於做正確的事情提供了SRC如下:
"Poster":"http://ia.media-imdb.com/images/M/[email protected]_V1_SX300.jpg"
當運行的代碼下面的圖片顯示,至少在Chrome和iOS的Safari瀏覽器,但在Chrome我得到的以下警告消息:Mixed Content: The page at 'https://run.plnkr.co/zv3BGVk31NLTM0Nh/' was loaded over HTTPS, but requested an insecure image 'http://ia.media-imdb.com/images/M/[email protected]@._V1_SX300.jpg'. This content should also be served over HTTPS.
這是代碼:
function getMovieInfo(myMoviesTitle, myMoviesYear, isLast) {
var xmlhttp = new XMLHttpRequest();
var url = "https://www.omdbapi.com/?i&t=" + myMoviesTitle + "&y=" + myMoviesYear + "&plot=short&r=json";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var movieInfo = JSON.parse(xmlhttp.responseText);
if (!movieInfo.Error) {
makeMovieList(myMoviesTitle, movieInfo);
}
if (isLast) {
// Runs DOM manipulation functions
}
}
};
xmlhttp.open('GET', url, true);
xmlhttp.send();
}
function makeMovieList(myMoviesTitle, movieInfo) {
// Appends information from omdbapi.com to DOM.
var movie = document.createElement('li');
movie.className = 'movie';
var thumbnail = document.createElement('img');
thumbnail.className = 'thumbnail';
thumbnail.src = movieInfo.Poster;
movie.appendChild(thumbnail);
我已經試過CORS爲好,這在Chrome上Plnkr作品一種享受(根本沒有錯誤消息),但似乎並沒有iOS上運行,或當我我的代碼上傳到Github上main.js:232 Mixed Content: The page at 'https://giles-taylor.github.io/js-movie-finder/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://www.omdbapi.com/?t=the+seven+year+itch&y=undefined&plot=short&r=json'. This request has been blocked; the content must be served over HTTPS.
function getMovieInfo(myMoviesTitle, myMoviesYear, isLast) {
var req = new XMLHttpRequest();
var url = 'http://www.omdbapi.com/?t=' + myMoviesTitle + '&y=' + myMoviesYear + '&plot=short&r=json';
if ('withCredentials' in req) {
req.open('GET', url, true);
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 400) {
var movieInfo = JSON.parse(req.responseText);
if (!movieInfo.Error) {
console.log(movieInfo);
makeMovieList(movieInfo);
}
if (isLast) {
// Runs DOM manipulation functions
}
} else {
// Handle error case
}
}
};
req.send();
}
}
function makeMovieList(myMoviesTitle, movieInfo) {
// as above
}
任何人都可以提出一個解決辦法,使這些圖像顯示安全(並且沒有控制檯警告)?謝謝!
所以使用'https:// www.omdbapi.com /'不起作用? - 因爲你說IMDB不允許https,但你使用http而不是https訪問ombdapi - 不確定IMDB在哪裏發揮作用 –
對不起,也許我沒有足夠清楚。無論您使用http://www.omdbapi.com/或http://www.omdbapi.com/ JSON中指定的圖像src都來自http。例如:「海報」:「http://ia.media-imdb.com/images/M/[email protected]_V1_SX300.jpg」。 – dedaumiersmith
啊,對不起,我的壞 –