Skip to content

Instantly share code, notes, and snippets.

@kitmenke
Created August 27, 2013 18:31
Show Gist options
  • Save kitmenke/6357230 to your computer and use it in GitHub Desktop.
Save kitmenke/6357230 to your computer and use it in GitHub Desktop.
Example for calling the Microsoft Translator AJAX API using jQuery
$(window).load(function () {
$('#btnAjaxTranslate').click(function (evt) {
// get the text the user wants to translate
var inputText = $('#txtAjaxInput').val();
// get the current auth token (comes from server side code)
var authToken = $('#txtAuthToken').val();
// translate from english to german
var data = {
appId: 'Bearer ' + authToken,
from: 'en',
to: 'de',
contentType: 'text/plain',
text: inputText
};
// note the dataType and jsonp properties
$.ajax({
url: "http://api.microsofttranslator.com/V2/Ajax.svc/Translate",
dataType: 'jsonp',
data: data,
jsonp: 'oncomplete'
})
.done(function (jqXHR, textStatus, errorThrown) {
console.log('done', this, jqXHR, textStatus, errorThrown);
// show the translation result to the user
$('#txtAjaxOutput').text(jqXHR);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log('fail', this, jqXHR, textStatus, errorThrown);
});
});
});
private static AdmAuthentication _admAuth = new AdmAuthentication(AdmConstants.CLIENT_ID, AdmConstants.CLIENT_SECRET);
protected void Page_Load(object sender, EventArgs e)
{
txtAuthToken.Text = _admAuth.GetAccessToken().access_token;
txtAuthExpires.Text = _admAuth.GetAccessToken().expires_in;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment