Created
August 27, 2013 18:31
-
-
Save kitmenke/6357230 to your computer and use it in GitHub Desktop.
Example for calling the Microsoft Translator AJAX API using jQuery
This file contains hidden or 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
$(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); | |
}); | |
}); | |
}); |
This file contains hidden or 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
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