我正在我的 Windows Phone 8 应用程序中利用 CamFind 的 API 进行图像识别。在他们的网站上,他们给出了如何将 API 与 Node.js 一起使用的示例。但是我正在编写 PhoneGap Windows Phone 应用程序,但没有可用的示例。
我想使用普通的 jquery/javascript 来使用此 API。
这是他们网站上提供的示例:
var Request = unirest.post("https://camfind.p.mashape.com/image_requests")
.headers({
"X-Mashape-Authorization": "Z**********************"
})
.send({
"image_request[locale]": "en_US",
"image_request[language]": "en",
"image_request[device_id]": "<image_request[device_id]>",
"image_request[latitude]": "35.8714220766008",
"image_request[longitude]": "14.3583203002251",
"image_request[altitude]": "27.912109375",
"focus[x]": "480",
"focus[y]": "640",
"image_request[image]": "/tmp/file.path"
})
.end(function (response) {
console.log(response);
});
这是我尝试使用 jquery/'plain' javascript 执行相同操作的方法
$.ajax({
url: 'https://camfind.p.mashape.com/image_requests', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
type: 'POST', // The HTTP Method
data: {
"image_request[locale]": "en_US",
"image_request[language]": "en",
"image_request[device_id]": "<image_request[device_id]>",
"image_request[latitude]": "35.8714220766008",
"image_request[longitude]": "14.3583203002251",
"image_request[altitude]": "27.912109375",
"focus[x]": "480",
"focus[y]": "640",
"image_request[image]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
}, // Additional parameters here
datatype: 'json',
success: function(data) { alert(JSON.stringify(data)); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "Z**********************");
}
});
问题:
- 当我通过 javascript/jquery 执行此操作时 - 它似乎在提示缺少 image_request[image] 属性。它永远不会触及成功的障碍。
- 我在如何转换 CamFind VS 提供的 Node.js API 请求示例(上面的第一个代码块)方面是否做错了什么?我如何尝试通过普通的 Javascript 使用 API(上面的第二个代码块)?
谢谢!!
仅供引用,我正在使用的引用文献:
- 在 javacstipt 中使用 API:https://www.mashape.com/imagesearcher/camfind#!endpoint-1-Image-Request CamFind API 使用:https://www.mashape.com/imagesearcher/camfind#!endpoint-1-Image-Request
最佳答案
我知道这是一个老问题,但在尝试自己解决它时偶然发现了它,我认为我应该为将来回答它。
问题是这一行:
"image_request[image]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
应该是:
"image_request[remote_image_url]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
所以完整的代码是:
$.ajax({
url: 'https://camfind.p.mashape.com/image_requests', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
type: 'POST', // The HTTP Method
data: {
"image_request[locale]": "en_US",
"image_request[language]": "en",
"image_request[device_id]": "<image_request[device_id]>",
"image_request[latitude]": "35.8714220766008",
"image_request[longitude]": "14.3583203002251",
"image_request[altitude]": "27.912109375",
"focus[x]": "480",
"focus[y]": "640",
"image_request[remote_image_url]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
}, // Additional parameters here
datatype: 'json',
success: function(data) { nowDoSomethingFun(data); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Key", "YOURKEY")
}
});
}
关于javascript - 在 javascript 中使用 API 而不使用 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20809693/