<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<p>This example shows how to post an image to the Project Oxford Emotion API using a binary file</p>
<input type="file" id="filename" name="filename">
<button id="btn">Click here</button>
<p id="response"></p>
JavaScript
var apiKey = "1dd1f4e23a5743139399788aa30a7153";
//apiUrl: The base URL for the API. Find out what this is for other APIs via the API documentation
var apiUrl = "https://api.projectoxford.ai/emotion/v1.0/recognize";
$('#btn').click(function () {
//file: The file that will be sent to the api
var file = document.getElementById('filename').files[0];
CallAPI(file, apiUrl, apiKey);
});
function CallAPI(file, apiUrl, apiKey)
{
$.ajax({
url: apiUrl,
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
},
type: "POST",
data: file,
processData: false
})
.done(function (response) {
ProcessResult(response);
})
.fail(function (error) {
$("#response").text(error.getAllResponseHeaders());
});
}
function ProcessResult(response)
{
var data = JSON.stringify(response);
$("#response").text(data);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.