gpt4 book ai didi

javascript - 通过 Javascript POST 到 Google Cloud Vision 的问题

转载 作者:行者123 更新时间:2023-12-03 03:57:42 25 4
gpt4 key购买 nike

我正在尝试通过带有 jquery 的 javascript 向 Google Cloud Vision API 进行非常简单的 POST。在 Chrome 中进行测试时,我通过控制台收到 400 错误,并且没有进一步的信息来帮助调试。我希望有人以前使用过 Cloud Vision,或者至少可以看到我在这里做了一些明显错误的事情,比如格式化请求正文(数据)。整个测试html/javascript如下:

<html><head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/json2/20160511/json2.js"></script>
<script type="text/javascript">

var p = {"requests":[{ "image":{ "source":{"imageUri":"https://cloud.google.com/vision/docs/images/car.png"}} , "features": [{"type":"LABEL_DETECTION","maxResults":3}] } ]};

$.ajax({
type: "POST",
url: "https://vision.googleapis.com/v1/images:annotate?key=APIKEY",
data: JSON.stringify(p),

headers: {
"Content-Type": "application/json",
},

dataType: "json",
success: function(data, textStatus, jqXHR) {
alert(data);
}

});

</script>
</head></html>

我一直在使用以下文档来寻求帮助: https://cloud.google.com/vision/docs/detecting-labels ,无济于事。

仅供引用,我也尝试过简写,但没有用,同样的错误:

var p = {"requests":[{  "image":{    "source":{"imageUri":"https://cloud.google.com/vision/docs/images/car.png"}}  ,  "features": [{"type":"LABEL_DETECTION","maxResults":3}]    } ]};
$.post( "https://vision.googleapis.com/v1/images:annotate?key=APIKEY", JSON.stringify(p) , function(data) { alert(data); } );

最佳答案

我能够毫无问题地完成以下工作,所以我不再关心上面的 jquery 解决方案:)

<script type="text/javascript">
var b=JSON.stringify({"requests":[{ "image":{ "source":{"imageUri":"https://cloud.google.com/vision/docs/images/car.png"}} , "features": [{"type":"LABEL_DETECTION","maxResults":5}] } ]});
var e=new XMLHttpRequest;

e.onload=function(){console.log(e.responseText)};
e.open("POST","https://vision.googleapis.com/v1/images:annotate?key=APIKEY",!0);
e.send(b)
</script>

关于javascript - 通过 Javascript POST 到 Google Cloud Vision 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44862532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com