Javascript React/Axios-“;代码";:400,”;“讯息”:&引用;所需参数:零件";
我正试图向我的youtube api发出GET请求,我得到了这个回复,尽管我已经包含了“部分”: 从网络选项卡:Javascript React/Axios-“;代码";:400,”;“讯息”:&引用;所需参数:零件";,javascript,reactjs,axios,Javascript,Reactjs,Axios,我正试图向我的youtube api发出GET请求,我得到了这个回复,尽管我已经包含了“部分”: 从网络选项卡: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Required parameter: part", "locationType": "parameter", "location": "part" } ],
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter: part",
"locationType": "parameter",
"location": "part"
}
],
"code": 400,
"message": "Required parameter: part"
}
}
有人能帮我找出我做错了什么吗
这是我的档案:
youtube.js
const KEY=“我的api密钥”;
导出默认的axios.create({
基本URL:“https://www.googleapis.com/youtube/v3",
参数:{
部分:“片段”,
最大结果:5,
钥匙:钥匙
}
});
我的应用程序:
类应用程序扩展了React.Component{
状态={};
onSearchSubmit=术语=>{
const x=youtube.get(“/search”{
参数:{
问:任期
}
});
控制台日志(x);
};
render(){
返回(
);
}
}
最后是我的SearchBar.js:
类搜索栏扩展React.Component{
状态={术语:};
onInputChange=事件=>{
this.setState({term:event.target.value});
};
onformSubmit=事件=>{
event.preventDefault();
this.props.onFormSubmit(this.state.term);
};
render(){
返回(
视频搜索
);
}
}
我一直在想办法,但运气不好。(我知道我没有保存fetch响应,我只是想在保存之前先测试GET。)
Axios-0.18
谢谢 原因:覆盖参数
const KEY = "my api key";
export const baseParams = {
part: "snippet",
maxResults: 5,
key: KEY
};
export default axios.create({
baseURL: "https://www.googleapis.com/youtube/v3"
})
import React from 'react';
import SearchBar from './SearchBar';
import youtube, { baseParams } from '../components/apis/youtube';
class App extends React.Component {
state = {};
onSearchSubmit = term => {
const x = youtube.get("/search", {
params: {
...baseParams,
q: term
}
});
console.log(x);
};
render() {
return (
<div className='ui container'>
<SearchBar onFormSubmit={this.onSearchSubmit} />
</div>
);
Axios v0.20.0版本解决了这个问题,这个问题在去年夏天发布 问题是v0.19.0中有一个bug无法合并配置实例中的参数。这将导致400错误故障,并显示消息“所需参数:零件”。所以你可以用几种方法来处理它
npm安装axios@0.20.0
onSearchSubmit
哦,对不起,数据实际上不存在。我只是在绝望中尝试一些东西,我已经编辑过了。但它仍然不起作用:(如果没有Anilm建议的params对象中的数据键,您会遇到相同的错误?这是正确的。这是我最初尝试的方式,然后我添加了数据,但忘记删除数据。当您发送搜索api请求时,能否查看“网络”选项卡并粘贴url?我完全复制了您的代码,它请求了此url:
“原因”:“keyInvalid”
在调用api时也使用异步等待
const KEY = "my api key";
export const baseParams = {
part: "snippet",
maxResults: 5,
key: KEY
};
export default axios.create({
baseURL: "https://www.googleapis.com/youtube/v3"
})
import React from 'react';
import SearchBar from './SearchBar';
import youtube, { baseParams } from '../components/apis/youtube';
class App extends React.Component {
state = {};
onSearchSubmit = term => {
const x = youtube.get("/search", {
params: {
...baseParams,
q: term
}
});
console.log(x);
};
render() {
return (
<div className='ui container'>
<SearchBar onFormSubmit={this.onSearchSubmit} />
</div>
);
const KEY = "my api key";
export default axios.create({
baseURL: "https://www.googleapis.com/youtube/v3",
params: {
part: "snippet",
maxResults: 5,
key: KEY
}
});