javascript - 图片加载失败

标签 javascript npm

来自浏览器控制台的错误:

 https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 404

尝试在浏览器上显示图片,不知道是我的代码问题还是food2fork端的问题。

我的 index.js:

// always make sure you have the right directory 

// import field 
import Search from './models/Search'; 
// import all the function from the view 
import * as searchView from './views/searchView'
import {elements} from './views/base'; 

/* Global state of the app
    - Search obj 
    - current recipe obj
    - shopping list object
    - liked recipes
*/

// everytime we reload the app, it will be empty 
const state = {}
const controlSearch = async () =>{
    // 1) Get the query from the view 
    const query =  searchView.getInput();

    if(query){
        // 2) new search object and add it to state 
        state.search = new Search(query); // new instance of the search class 

        // 3) prepare UI for results 

        // 4) Search for recipes 
        await state.search.getResults(); // await this promise then render the result 

        // 5) render result in the UI, reminder u got hit the search button 
        searchView.renderResult(state.search.result);

    }
}
elements.searchForm.addEventListener('submit', e => {
    e.preventDefault();
    controlSearch();
});

我的 Search.js:

// this is the external source simply call its name
import axios from 'axios';

// query and then the search result 
// class declarition ES6
export default class Search { 
    constructor(query){
        this.query = query;
    }

    async getResults(){
        // fetch is only gonna work for modern browser 
        // HTTP request axios 
        // if you enter the invalid the key it will not work
//key is blurred out for stackoverflow
        const key = '------------------------';

        // return json 
        // if we can not access it we are going to use the cors proxy
       // const proxy = you can use google to search for cors proxy
        try{
            const res = await axios(`https://www.food2fork.com/api/search?key=${key}&q=${this.query}`);
            this.result = res.data.recipes;
            // console.log(this.result);
        } catch(error){
            alert(error);
        }
    }


}

我的 searchView.js:

// if we are in the current folder then it is simply base 
import {elements} from './base'; 
// return the input value from the field 
// implicit search automatically return 
export const getInput =() => elements.searchInput.value; 

const renderRecipe = recipe =>{
    const markup = `
        <li>
            <a class="results__link" href="#${recipe.recipe_id}">
                <figure class="results__fig">
                    <img src="${recipe.image_url}.jpg" alt=${recipe.title}>
                </figure>
                <div class="results__data">
                    <h4 class="results__name">${recipe.title}</h4>
                    <p class="results__author">${recipe.publisher}</p>
                </div>
             </a>
        </li>
    `;
    // insert the html
    elements.searchResList.insertAdjacentHTML('beforeend',markup);

}

export const renderResult = recipes => {
    recipes.forEach(renderRecipe);
}

我的 base.js:

// all the DOM element will be in this class object
export const elements = {
    searchForm: document.querySelector('.search'),
    searchInput: document.querySelector('.search__field'),
    searchResList: document.querySelector('.results__list')
}

我是网络开发的新手,正在自学。我希望这不是一个糟糕的问题。我需要有经验的人帮我看看这个错误,因为它不是语法或逻辑错误。非常感谢,祝你有美好的一天。

最佳答案

https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 您是要添加 .jpg.jpg 吗?..如果不是,请删除最后一个 .jpg

https://static.food2fork.com/pastaallavodkaa870.jpg

关于javascript - 图片加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56761879/

相关文章:

jquery - 将应用程序迁移到 Angular 6 : But getting errors while running npm run build --prod. 但命令 npm run build --env=prod 成功运行

node.js - 使用 SSH 部署 mupx 期间出现未处理的错误(生成 ENOENT)

node.js - 每个命令上的 npm 错误 : EEXIST: file already exists, mkdir 'c:\users\user\appdata\Roaming\npm'

heroku - npm 以 NODE_ENV 作为参数运行

javascript - 如何制作带有固定标题的可滚动表格

javascript - 如何使用 parseInt 函数读取值零

javascript - 如何防止在调整大小时触发 onscroll 函数?

javascript - 如何防止javascript中的重复条目?

javascript - Leaflet:隐藏/显示动态插入 map 中的标记

node.js - TypeScript 编译中缺少 Carousel.d.ts。请通过 'files' 或“include”属性确保它位于您的 tsconfig 中