I have the following CURL Command which I want to convert to Jquery ajax but it alerts error Cannot get data
In the browser console. it shows error 401 Unathourized
I tried leveraging solution found here Source but still cannot get it to work
curl -X POST https://developer.expert.ai/oauth2/token \
-H 'Content-Type: application/json; charset=utf-8' \
-d '{
"username": "yourusername",
"password": "yourpassword"
}'
Here is the coding so far
$.ajax({
url: "https://developer.expert.ai/oauth2/token",
beforeSend: function(xhr) {
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
},
type: 'POST',
crossDomain: true,
dataType: 'json',
contentType: 'application/json',
processData: false,
data: JSON.stringify({
'username': 'api username goes here',
'password': 'api password goes here'
}),
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});
errorcallback?error: (_, ...err) => { console.error("Cannot get data", ...err); }would tell you a lot more about what went wrong. You could also use your browser dev-tools Network panel to inspect the request and response (if any)errorcallback? Errors don't just happen silently. Please also consult the documentationdataType: 'json',todataType: 'html', and it works