Skip to content

Instantly share code, notes, and snippets.

@andijakl
Created March 20, 2019 04:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andijakl/542621e849314972adaac8451dbd51f2 to your computer and use it in GitHub Desktop.
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