我正在尝试通过chrome扩展程序获取文本并将其发布到Google API。
此刻我收到此错误。
POST https://language.googleapis.com/v1/documents:analyzeSentiment?key=PASSWORD 400
Response {type: "cors", url: "https://language.googleapis.com/v1/documents:analy…iment?key=strongPassword", redirected: false, status: 400, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 400
statusText: ""
type: "cors"
url: "https://language.googleapis.com/v1/documents:analyzeSentiment?key=Strongk"
__proto__: Response
我尝试使用XMLHttpRequest代替fetch,并通过下面的代码(使用了fetch)进行了测试。
function sendData(text) {
console.log(text);
// json p?
var request = {
document: {
type: 'PLAIN_TEXT',
content: text
},
encodingType: 'UTF8'
};
//use another type of call?
var jsonRequest = JSON.stringify(request)
console.log("JSON STRING: "+jsonRequest)
fetch('https://language.googleapis.com/v1/documents:analyzeSentiment?key=StrongkPword', {
mode:'cors',
method: 'POST',
body: jsonRequest,
headers: {
'Content-Type': 'application/json'
}
}).then((res) => {
console.log(res)
console.log(res.body)
});
}
所以我目前的想法是,需要使用JsonP,否则还有其他不正确的地方。
要发送的文字是
JSON STRING: {"document":{"type":"PLAIN_TEXT","content":"GIVE IT A STAB.. YEAH...PLZWORK "},"encodingType":"UTF8"}
{
"manifest_version": 2,
"name": "C.E",
"short_name": "MRPHASMD",
"version": "1.0",
"author": "MICHAEL HOLBORN",
"description": "A minimal extension which demonstrates ma power level.",
"content_scripts": [
{
"matches": [ "*://mail.google.com/*" ],
"js": [
"src/extensionInjector.js"
],
"run_at": "document_end"
}
],
"web_accessible_resources": [
"dist/extension.js"
],
"permissions": [
"https://*/*"
]
}
对于如何使它正常工作并感谢您的帮助,我感到很困惑!
答案 0 :(得分:0)
我通过利用chrome API将消息发送到后台脚本功能,然后发送到我的API,解决了这个问题