Skip to content

Instantly share code, notes, and snippets.

@namsor
Created May 24, 2018 11:53
Show Gist options
  • Save namsor/bc1549c209c1ffcb3ffdc9f399fa917f to your computer and use it in GitHub Desktop.
Save namsor/bc1549c209c1ffcb3ffdc9f399fa917f to your computer and use it in GitHub Desktop.
jQuery : Inferring Gender 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/gender/John/Smith',
//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.
},
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.
}
});
// returns {"scale":-1.0,"gender":"male","firstName":"John","lastName":"Smith","id":"1527163531916"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment