Javascript 如何转换Axios以有效获取数据?
我正在尝试实现microsoft翻译API。()Javascript 如何转换Axios以有效获取数据?,javascript,node.js,azure,axios,fetch,Javascript,Node.js,Azure,Axios,Fetch,我正在尝试实现microsoft翻译API。() 我想将它与对话流集成,而对话流与axios函数不兼容,所以我尝试使用异步函数fetch 此代码运行良好 axios({ "method": "POST", "url": "https://microsoft-translator-text.p.rapidapi.com/translate", "headers": {
我想将它与对话流集成,而对话流与axios函数不兼容,所以我尝试使用异步函数fetch
此代码运行良好
axios({
"method": "POST",
"url": "https://microsoft-translator-text.p.rapidapi.com/translate",
"headers": {
"content-type": "application/json",
"x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
"x-rapidapi-key": "key",
"accept": "application/json",
"useQueryString": true
}, "params": {
"profanityAction": "NoAction",
"textType": "plain",
"to": "hi",
"api-version": "3.0"
}, "data": [{
"Text": "Hello, World"
}]
})
.then((response) => {
console.log(response);
console.log(response.data[0].detectedLanguage.language);
console.log(response.data[0].translations[0].text);
})
.catch((error) => {
console.log(error);
})
但是当我使用fetch使用相同的代码时,它给出了错误
async function translate() {
const response = await fetch(`https://microsoft-translator-text.p.rapidapi.com/translate`, {
"method": "POST",
"headers": {
"content-type": "application/json",
"x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
"x-rapidapi-key": "key",
"accept": "application/json",
"useQueryString": true
}, "params": {
"profanityAction": "NoAction",
"textType": "plain",
"to": "hi",
"api-version": "3.0"
}, "data": [{
"Text": "Hello, World"
}]
});
const jsonres = await response.json();
console.log(jsonres);
}
translate();
error:
code: 400000
message: "One of the request inputs is not valid."
这就是错误所在
async function translate() {
const response = await fetch(`https://microsoft-translator-text.p.rapidapi.com/translate`, {
"method": "POST",
"headers": {
"content-type": "application/json",
"x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
"x-rapidapi-key": "key",
"accept": "application/json",
"useQueryString": true
}, "params": {
"profanityAction": "NoAction",
"textType": "plain",
"to": "hi",
"api-version": "3.0"
}, "data": [{
"Text": "Hello, World"
}]
});
const jsonres = await response.json();
console.log(jsonres);
}
translate();
error:
code: 400000
message: "One of the request inputs is not valid."
我已经试过你的代码了,没关系。我认为,这个问题可能是您没有使用
节点获取
因为我没有订阅此api,所以我的演示代码得到的响应消息是您没有订阅此api。
您需要在文件中创建一个带有{}
的package.json。然后您可以创建test.js
文件并粘贴我的代码
运行:
1.npm i
2.npm i axios
3.npm i节点获取
4.node test.js
因为fetch使用不同的参数格式。请阅读:你的问题解决了吗?如果您需要进一步的帮助,请让我知道。是的,非常感谢,问题已经解决。