Headers not sent with ajax request

John

In my React.js app I use the jquery .ajax method to retrieve info from a backend API. To do this I need to add a authorization token as a header and I added the headers option:

$.ajax({
  url: this.props.source,
  headers: { 'Accept':'application/json', 'Authorization':'Token 36d417' },
  dataType: 'jsonp',
  success: function(data) {
    this.setState({data: data});
  }.bind(this),
  error: function(xhr, status, err) {
    console.error(this.props.url, status, err.toString());
  }.bind(this)
});

But I receive a 401 Unauthorized response from the server. When I check the headers they don't seem to be added?

Remote Address:189.28.154.73:8080
Request URL:https://todos-api.com/api/v1/todos?callback=jQuery1100005772984796203673_1424179650018&_=1424179650019
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6,nl;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:todos-api.com
Pragma:no-cache
Referer:https://todos-reactjs.io/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
bhspencer

Try using the beforeSend callback

        $.ajax({
              url: this.props.source,
              beforeSend: function (xhr){ 
                xhr.setRequestHeader('Authorization', 'Token 36d417'); 
                xhr.setRequestHeader('Accept', 'application/json'); 
              },
              dataType: 'jsonp',
              success: function(data) {
                this.setState({data: data});
              }.bind(this),
              error: function(xhr, status, err) {
                console.error(this.props.url, status, err.toString());
              }.bind(this)
            });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ajax request not being sent

From Dev

AJAX request with headers failing

From Dev

Referencing data sent in an AJAX request

From Dev

Set headers for AJAX request to Django

From Dev

'Authorization' header sent with request, but missing from apache_request_headers()

From Dev

Ajax request not sent error unknown in Firefox

From Dev

X-Editable AJAX request not being sent

From Dev

Authorization header not being sent in AJAX request

From Dev

jquery ajax : what is the actual request sent?

From Dev

How to check if Ajax request was sent in Capybara Rspec?

From Dev

Bypassing authentication for "Options request" (so all headers are sent in the response)

From Dev

Can't set headers after they are sent after first request only

From Dev

"Can't set headers after they are sent" and empty post request body

From Dev

Ionic 2 - Angular 2 http Headers are not being sent along with the request

From Dev

Few HTTP headers missing for the request sent via java

From Dev

Error: Can't set headers after they are sent in loop multiple request

From Dev

Send Headers in Cross domain request From Ajax

From Dev

GitHub: How to set headers for ajax request?

From Dev

What are the Ajax authorization headers for a Bing API request?

From Dev

Custom headers on any ajax request using MooTools

From Dev

setting multiple request headers with jquery ajax

From Dev

Ajax cross domain request fail to set headers

From Dev

Why there are different request headers for same ajax call?

From Dev

detect specific ajax request (data sent) from multiple request

From Java

Access Control Request Headers, is added to header in AJAX request with jQuery

From Dev

How can I get the http request headers actually sent by me in node.js http request

From Dev

How can I get the http request headers actually sent by me in node.js http request

From Dev

Request not sent

From Dev

Downloading a file sent in response to a POST request via nodejs? (no AJAX)

Related Related

  1. 1

    Ajax request not being sent

  2. 2

    AJAX request with headers failing

  3. 3

    Referencing data sent in an AJAX request

  4. 4

    Set headers for AJAX request to Django

  5. 5

    'Authorization' header sent with request, but missing from apache_request_headers()

  6. 6

    Ajax request not sent error unknown in Firefox

  7. 7

    X-Editable AJAX request not being sent

  8. 8

    Authorization header not being sent in AJAX request

  9. 9

    jquery ajax : what is the actual request sent?

  10. 10

    How to check if Ajax request was sent in Capybara Rspec?

  11. 11

    Bypassing authentication for "Options request" (so all headers are sent in the response)

  12. 12

    Can't set headers after they are sent after first request only

  13. 13

    "Can't set headers after they are sent" and empty post request body

  14. 14

    Ionic 2 - Angular 2 http Headers are not being sent along with the request

  15. 15

    Few HTTP headers missing for the request sent via java

  16. 16

    Error: Can't set headers after they are sent in loop multiple request

  17. 17

    Send Headers in Cross domain request From Ajax

  18. 18

    GitHub: How to set headers for ajax request?

  19. 19

    What are the Ajax authorization headers for a Bing API request?

  20. 20

    Custom headers on any ajax request using MooTools

  21. 21

    setting multiple request headers with jquery ajax

  22. 22

    Ajax cross domain request fail to set headers

  23. 23

    Why there are different request headers for same ajax call?

  24. 24

    detect specific ajax request (data sent) from multiple request

  25. 25

    Access Control Request Headers, is added to header in AJAX request with jQuery

  26. 26

    How can I get the http request headers actually sent by me in node.js http request

  27. 27

    How can I get the http request headers actually sent by me in node.js http request

  28. 28

    Request not sent

  29. 29

    Downloading a file sent in response to a POST request via nodejs? (no AJAX)

HotTag

Archive