Skip to content

Instantly share code, notes, and snippets.

@namsor
Created May 24, 2018 11:56
Show Gist options
  • Save namsor/cf026ab5cca36343e63092fb77bd6e0d to your computer and use it in GitHub Desktop.
Save namsor/cf026ab5cca36343e63092fb77bd6e0d to your computer and use it in GitHub Desktop.
jQuery : Inferring Origin of a Personal Name using NamSor API and Cross-Origin Resource Sharing (CORS)
// Submit the form using AJAX.
$.ajax({
type: 'GET',
url: 'https://api.namsor.com/onomastics/api/json/origin/Elena/Rossini',
//contentType: 'text/json',
xhrFields: {
// The 'xhrFields' property sets additional fields on the XMLHttpRequest.
// This can be used to set the 'withCredentials' property.
// Set the value to 'true' if you'd like to pass cookies to the server.
// If this is enabled, your server must respond with the header
// 'Access-Control-Allow-Credentials: true'.
withCredentials: false
},
headers: {
// Set any custom headers here.
// If you set any non-simple headers, your server must include these
// headers in the 'Access-Control-Allow-Headers' response header.
'X-Channel-Secret': '<your API Key here>',
'X-Channel-User': '<your API User/channel here> namsor.com/yourname@company.com/1235'
},
success: function() {
// Here's where you handle a successful response.
},
error: function() {
// Here's where you handle an error response.
// Note that if the error was due to a CORS issue,
// this function will still fire, but there won't be any additional
// information about the error.
}
});
//This returns JSON,
//{"country":"IT","countryAlt":"CY","score":2.6901979931277546,"script":"LATIN","countryFirstName":"RU","countryLastName":"IT","scoreFirstName":3.944646495195318,"scoreLastName":2.1005188527727787,"subRegion":"Southern Europe","region":"Europe","topRegion":"Europe","countryName":"Italy"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment