我试图从https://suggestqueries.google.com/complete/search?
获取一个api请求,但它正在返回此错误"未捕获的语法错误:意外的标记"<"&";。这是我的代码
import React, { useState } from 'react';
import suggestSearch from "../../Apis/suggestSearch";
const getSuggestions = () => {
const [autoSearchData, setAutoSearch] = useState("");
const getAutocomplete = async () => {
const response = await suggestSearch.get("", {
params: {
q: searchQuery,
}
});
setAutoSearch(response.data[1]);
console.log(setAutoSearch);
console.log(autoSearchData);
};
}
这是建议的搜索代码
import axios from "axios";
import jsonpAdapter from "axios-jsonp";
export default axios.create({
baseURL: "https://suggestqueries.google.com/complete/search?",
adapter: jsonpAdapter,
params: {
hl: "en", // Language
ds: "yt", // Restrict lookup to youtube
client: "youtube" // force youtube style response, i.e. jsonp
}
});
发生此错误是因为您引用的资产已不存在。检查您的API请求链接,看看它是否仍然是有效的资产。
根据baseURL
,我推断您正在尝试执行application/x-www-form-urlencoded
请求
要使这样的请求在axios
中成功,您必须导入querystring
模块,然后执行axios.post(baseURL,qs.stringify({data}),config)
在这种情况下,const qs=require('querystring');
。