gpt4 book ai didi

javascript - 我的提取请求不断收到错误 "the string did not match the expected pattern"

转载 作者:行者123 更新时间:2023-12-03 11:25:17 27 4
gpt4 key购买 nike

对于我的提取请求,我不断收到错误“字符串与预期的模式不匹配”​​。我在这里和其他论坛上看到一些人有类似的问题,但无法查明问题所在。如果有人有任何建议,请告诉我。

function showRecipes(){
const ingredients = document.getElementById('ingredients').value.replace(/\s/g, "");
const meal = document.getElementById('meal').value;
const display = document.getElementById('display'); //Where results will go

let url = 'http://www.recipepuppy.com/api/';
url += `?i=${ingredients}&q=${meal}`;

fetch(url, { mode: "no-cors"})
.then(response => {
response.json()
.then(data => {
data.results.forEach(recipe => {
const container = document.createElement('div');
container.setAttribute('class', 'container');

const h1 = document.createElement('h1');
h1.textContent = recipe.title;

const p = document.createElement('p');
p.textContent = recipe.ingredients;

display.appendChild(container);
container.appendChild(h1);
container.appendChild(p);
})
})
.catch(err => {
console.log(err);
})
})
.catch(err => {
console.log('ERROR: ' + err);
})
}

最佳答案

如果服务器的响应是文本,但您尝试使用 res.json() 将其解析为 JSON,您可能会收到此错误。 .

fetch(serverUrl, {method: 'GET'})
.then((res) => res.json())
res.text()如果服务器返回文本是合适的。
在这种情况下,Safari 曾经给我 OP 的错误,但 Chrome 更具体:“位置 0 处的 json 中的意外 token W”—— res.json()期望字符串的第一个字符是 {[因为这就是 JSON 的开始。
或者,正如 Safari 所说,“字符串与预期的模式不匹配”​​。

关于javascript - 我的提取请求不断收到错误 "the string did not match the expected pattern",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com