Call to the Azure Cognitive Services Computer Vision API from JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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