-1

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");
    }
});
4
  • 1
    Have you tried debugging the actual error passed to your error callback? 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) Commented Oct 16, 2023 at 2:47
  • @Phil thanks for getting back to me. I check network tab. I can see 2 url index.html which contains the ajax-jquery code and developer.expert.ai/oauth2/token which displays result token. Please what do I do. I need to get the result token alerted or printed on index.html page Commented Oct 16, 2023 at 5:49
  • What about the Console tab, any errors reported there? What about the changes I suggested you make to your error callback? Errors don't just happen silently. Please also consult the documentation Commented Oct 16, 2023 at 5:52
  • Thank you so much. I change dataType: 'json', to dataType: 'html', and it works Commented Oct 16, 2023 at 9:55

1 Answer 1

-1

The issue was solved by changing dataType Parameter from json to html as per dataType: 'json', to dataType: 'html', and it works

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.