我使用Google翻译API来翻译电子邮件正文。帐单也添加到了API密钥中。 我在进行api调用时遇到404-找不到错误。
var apiKey = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
var endpoints = {
translate: "",
detect: "detect",
languages: "languages"
};
function makeApiRequest(endpoint, data, type, authNeeded) {
url = "https://translation.googleapis.com/language/translate/v2/"+ endpoint;
url += "?key=" + apiKey;
// If not listing languages, send text to translate
if (endpoint !== endpoints.languages && endpoint !== endpoints.detect)
{
url += "&q=" + encodeURI(data.textToTranslate1);
url += "&q=" + encodeURI(data.textToTranslate2);
}
if (endpoint === endpoints.detect) {
url += "&q=" + encodeURI(data.textToTranslate1);
}
// If translating, send target and source languages
if (endpoint === endpoints.translate) {
url += "&target=" + data.targetLang;
url += "&source=" + data.sourceLang;
url+="&format=text";
}
jQuery.support.cors = true;
// Return response from API
return $.ajax({
url: url,
type: type || "GET",
data: data ? JSON.stringify(data) : "",
dataType: "JSON",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
}
});
}
function translate(data) {
makeApiRequest(endpoints.translate, data,
"GET",false).success(function(resp) {
//the code
});
.on("click", "button.translate", function() {
var translationObj = {};
translationObj = {
sourceLang: $(".source-lang").val(),
targetLang: $(".target-lang").val(),
textToTranslate1: "Good Morning",
textToTranslate2: "Good Night"
};
translate(translationObj);
}
这是iam使用的api请求代码。 一旦我清除缓存并重新加载应用程序5-6次,则api才会响应。
您能在这里帮我吗
答案 0 :(得分:0)
Docs say:404 NOT_FOUND找不到指定的资源,或者由于未公开的原因(例如白名单),请求被拒绝。
我认为这行可能是错误的:url =“ https://translation.googleapis.com/language/translate/v2/” +端点;
,因为您要添加其他文本,使端点与预期的端点不同。根据{{3}},引用的文字应足够。
添加api密钥的行似乎是正确的:url + =“?key =” + apiKey;