1

i have request

$(function(){
    $.ajax({
        type: 'GET',
        dataType:"jsonp",
        url: "http://localhost:8000/api/admin/announces",       
        headers:{         
            'Authorization' : 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBWZXIiOiIwLjAuMCIsImV4cCI6NDcyNjM4OTEyMiwibG9jYWxlIjoiIiwibWFzdGVyVmVyIjoiIiwicGxhdGZvcm0iOiIiLCJwbGF0Zm9ybVZlciI6IiIsInVzZXJJZCI6IiJ9.QIZbmB5_9Xlap_gDhjETfMI6EAmR15yBtIQkWFWJkrg',
            'Content-Type':'application/json'
        },
        succces: function(data){
            console.log('success',data);                    
        }
    });
});

and i having 400 bad request error net::ERR_ABORTED 400 (Bad Request). What am I missing ?

6
  • Can you try from your browser. Also check cross origin policy in your server. Server cors should allow your localhost:8000. Commented Nov 25, 2019 at 8:58
  • 400 - is something resource not found, check API url and method type bcoz rest looks good in you jquery ajax Commented Nov 25, 2019 at 8:58
  • Check the server why it rejects your request with status 400. Commented Nov 25, 2019 at 9:00
  • You can check for url is working or not, by direct hit on browser, or check for method get or post , and also try dataType:'json' Commented Nov 25, 2019 at 9:00
  • my API check on POSTMAN is ok , but browser not ok . i think is token . my code backend send request have token ? so , what am i missing in Ajax Commented Nov 25, 2019 at 9:04

2 Answers 2

3

You just need to replace URL with your local REST API:

$.ajax({
  type: 'GET',
    dataType:"jsonp",
  url: 'https://jsonplaceholder.typicode.com/todos/1',
  headers:{         
      'Authorization' : 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBWZXIiOiIwLjAuMCIsImV4cCI6NDcyNjM4OTEyMiwibG9jYWxlIjoiIiwibWFzdGVyVmVyIjoiIiwicGxhdGZvcm0iOiIiLCJwbGF0Zm9ybVZlciI6IiIsInVzZXJJZCI6IiJ9.QIZbmB5_9Xlap_gDhjETfMI6EAmR15yBtIQkWFWJkrg',
      
  },
  success: function (data, status, xhr) {
    console.log('data: ', data);
  }
});

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

Comments

2

When a server cannot understand a request, its called 400 error. There are few reason for it. Maybe you have used a url with invalid token or cookie. Please make sure ur bearer token is valid.. Clear ur DNS cache, browser cache..i think its your bearer token.. Please make sure your bearer token is valid..

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.