JavaScript中pythons requests.post方法的等价物是什么?
我尝试用Javascript连接谷歌透视API。我已经尝试了很多我研究过的东西,但都不管用。但是当我在Python中尝试它时,它非常简单。。。 因此,基本上我需要JavaScript中与以下简单Python代码等效的代码。它必须在浏览器插件中工作JavaScript中pythons requests.post方法的等价物是什么?,javascript,python,ajax,plugins,python-requests,Javascript,Python,Ajax,Plugins,Python Requests,我尝试用Javascript连接谷歌透视API。我已经尝试了很多我研究过的东西,但都不管用。但是当我在Python中尝试它时,它非常简单。。。 因此,基本上我需要JavaScript中与以下简单Python代码等效的代码。它必须在浏览器插件中工作 import json import requests api_key = '<MYAPIKEY>' url = ('https://commentanalyzer.googleapis.com/v1alpha1/comments:anal
import json
import requests
api_key = '<MYAPIKEY>'
url = ('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'+'?key='+api_key)
data_dict = {
'comment': {'text': 'text to be checked'},
'languages': ['en'],
'requestedAttributes': {'TOXICITY': {}}
}
response = requests.post(url=url, data=json.dumps(data_dict))
response_dict = json.loads(response.content)
导入json
导入请求
api_键=“”
url=('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze“+”?键=“+api_键)
数据记录={
'comment':{'text':'text to check'},
'语言':['en'],
'请求的属性':{'毒性':{}
}
response=requests.post(url=url,data=json.dumps(data_dict))
response\u dict=json.load(response.content)
我不是JavaScript专家,我真的需要帮助来解决这个问题,我希望有人能帮助我。是您需要的,它由核心js模块提供。显示用法。您可以使用
const-api\u-key='';
常量url=('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze“+”?键=“+api_键);
常量数据\u dict={
'comment':{'text':'text to check'},
'语言':['en'],
'请求的属性':{'毒性':{}
};
获取(url,{
方法:“POST”,
正文:JSON.stringify(data_dict)
})
.then(response=>response.json())
。然后(数据=>{
//对响应中的数据执行任何操作
});
您必须使用fetch(或axios),如:
使用fetch(es6)返回承诺,所以您可能需要。您可以使用fetch(或axios)调用api:谢谢,我比以前更接近了。但是当我记录
数据时
只是说[object object]
?
const api_key = '<MYAPIKEY>';
const url = ('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'+'?key='+api_key);
const data_dict = {
'comment': {'text': 'text to be checked'},
'languages': ['en'],
'requestedAttributes': {'TOXICITY': {}}
};
fetch(url, {
method: 'POST',
body: JSON.stringify(data_dict)
})
.then(response => response.json())
.then(data => {
// do whatever with the data from the response
});
fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
body: JSON.stringify(data),
}).then(res => res.json())
.then(data => console.log(data))
.catch(err => console.log(err));