我在这里遵循文档,底部的示例代码如下所示
<!DOCTYPE html>
<html>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
"returnFaceId": "true",
"returnFaceLandmarks": "false",
"returnFaceAttributes": "{age}",
};
$.ajax({
url: "https://api.projectoxford.ai/face/v1.0/detect?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","REDACTED");
},
type: "POST",
// Request body
data: "http://newsrescue.com/wp-content/uploads/2015/04/happy-person.jpg",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>
但是我一直收到错误代码404找不到资源。谁能告诉我我做错了什么?
使用Postman快速检查显示您得到的参数错误(不是404)。
几件事:
-
returnFaceAttributes
应该是"age"
的(而不是"{age}"
) -
data
需要一个参数名称
试试这个(检查data
更新):
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<title>JSSample</title>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
"returnFaceId": "true",
"returnFaceLandmarks": "false",
"returnFaceAttributes": "age",
};
$.ajax({
url: "https://api.projectoxford.ai/face/v1.0/detect?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","e2c75ad5d44846d590ac7c2dcc2f210e");
},
type: "POST",
// Request body
data: '{ "url": "http://newsrescue.com/wp-content/uploads/2015/04/happy-person.jpg"}'
})
.done(function(data) {
console.log(data);
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>
我创建了一个 jsbin 以确保它有效(根据 Microsoft,她是 19.3 岁)。
最后一个重要说明。立即更改您的Ocp-Apim-Subscription-Key
密钥!