0

I am using Django REST framework to create an API. It is working fine with cURL.

curl -H 'Accept: application/json; indent=4' -u ratan:qazwsxedc123 http://127.0.0.1:8000/api/grocery/subcategories/

However, when I try to use jQuery to populate a div in a HTML page it is not working, I am getting a 403 forbidden error.

This is my code.

$.ajax({
  url: "http://127.0.0.1:8000/api/grocery/subcategories/?format=json",
  type: "GET",
  dataType: "json",
  data: {
    username: "ratan",
    password: "qazwsxedc123"
  },
  success: function(data) {
    console.log('ratan');
  },
});

I thought it can be because of CORS, so I have tried django-cors-headers, but it didn't help.

3
  • try adding contentType: "application/json", as it looks like you're trying to send a json object. But then again in your CURL you are sending them in a header. Commented Feb 24, 2016 at 22:15
  • This really isn't a Django question. Commented Feb 24, 2016 at 22:15
  • @DanielRoseman The problem was in his JS, but it's good to know all the details for context. He could have misconfigured his DRF somewhere. Commented Feb 24, 2016 at 22:17

1 Answer 1

1

You are passing your username and password incorrectly when making the request. This is discussed in this SO question.

Try this method instead.

$.ajax
({
  type: "GET",
  url: "http://127.0.0.1:8000/api/grocery/subcategories/?format=json",
  dataType: 'json',
  headers: {
    "Authorization": "Basic " + btoa("ratan:qazwsxedc123")
  },
  success: function (){
    console.log('ratan'); 
  }
});
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.