Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Call to the Azure Cognitive Services Computer Vision API from JavaScript
$.ajax({
// TODO: if needed, adapt the URL to the region where you created the service
url: "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze?" + $.param(params),
beforeSend: function(xhrObj){
// Request the output formatted as JSON
xhrObj.setRequestHeader("Content-Type","application/json");
// Send the subscription key as part of the header
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
},
// Data is submitted to the server through a POST request
type: "POST",
// Request body: the image URL
data: '{"url": ' + '"' + sourceImageUrl + '"}',
})
.done(function(data) {
// Successful request: formate the result JSON in a human-friendly
// way and output it to the text area HTML element.
responseTextArea.val(JSON.stringify(data, null, 2));
alert("Success");
})
.fail(function(jqXHR, textStatus, errorThrown) {
// Error: print the error message to the text area HTML element.
responseTextArea.val(JSON.stringify(jqXHR, null, 2));
alert("Request Failed");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.