Javascript 为什么使用axios获取数据时出现错误500
我正在做一个小项目来学习,并把它放在我的项目部分 我遇到这样的问题: 获取500(内部服务器错误) 未捕获(承诺中)错误:请求失败,状态代码为500 在createError(createError.js:16) 结算时(结算js:17) 在XMLHttpRequest.handleLoad(xhr.js:61) 我尝试使用axios头文件(我在另一个论坛上发现了这个建议,但它不起作用) 我获取数据的代码如下所示Javascript 为什么使用axios获取数据时出现错误500,javascript,async-await,xmlhttprequest,axios,httprequest,Javascript,Async Await,Xmlhttprequest,Axios,Httprequest,我正在做一个小项目来学习,并把它放在我的项目部分 我遇到这样的问题: 获取500(内部服务器错误) 未捕获(承诺中)错误:请求失败,状态代码为500 在createError(createError.js:16) 结算时(结算js:17) 在XMLHttpRequest.handleLoad(xhr.js:61) 我尝试使用axios头文件(我在另一个论坛上发现了这个建议,但它不起作用) 我获取数据的代码如下所示 export async function fetchData(text) {
export async function fetchData(text) {
const proxy = `https://cors-anywhere.herokuapp.com/`;
const base = `http://www.recipepuppy.com/api/`;
const baseEnd = `q=${text}`;
const data = await axios
.get(`${proxy}${base}?${baseEnd}`)
.then(data =>console.log(data));
const recipes = data.data.results;
return recipes.length > 0 ? recipes : false;
}
此处调用的函数:
async function getRecipes(e){
e.preventDefault();
const text = e.target[1].value;
const loader = '<div class="loader"></div>';
container.insertAdjacentHTML('beforebegin', loader);
const recipes = await fetchData(text);
document.querySelector('.loader').remove();
displayRecipes(recipes);
}
异步函数getRecipes(e){
e、 预防默认值();
const text=e.target[1]。值;
常量加载器=“”;
container.insertAdjacentHTML('beforebeagin',loader);
const recipes=等待获取数据(文本);
document.querySelector('.loader').remove();
显示配方(配方);
}
您需要从Promise捕获可能的错误(如axios.get()返回Promise类型对象)
如果仍然存在该错误,请添加该函数的完整代码以及调用该函数的部分代码。这是我调用fetch的代码
导出异步函数fetchData(text){const proxy=
;const base=
?;const baseEnd=
q=${text};const data=wait axios.get(
${proxy}${base}?${baseEnd}).catch(err=>console.log(err));console.log(data)return recipes.length>0?recipes:false;}
在这里调用函数`在这里调用异步函数getRecipes(e){e.preventDefault();const text=e.target[1].value;const loader='';container.insertAdjacentHTML('beforebeagin',loader);const recipes=wait fetchData(text);document.querySelector('.loader').remove();displayRecipes(recipes);}这在两天前就开始工作了,昨天我想编写我的小应用程序,但它甚至没有获取数据,因此错误提示您至少有3个脚本。您可能正在调用getRecepies()来自另一个异步函数,它也包装在Promise中?几天前,我在Vuejs中反对授权流,并遇到了相同的错误。因此,为了本地化它,我将所有逻辑移到了“根”函数中,然后一个接一个地开始将部分代码移到相对的“子”函数中。问题在某种程度上丢失了。catch()。请尝试{await axios.get(${proxy}${base}?${baseEnd}
)}catch(err){console.log(err)}——知道为什么不使用await处理这个问题吗。
axios.get(`${proxy}${base}?${baseEnd}`)
.then(data =>console.log(data))
.catch(error => {
console.log(error)
})