question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hook for modifying XHR before JSON feed request

See original GitHub issue

Hello,

I’m trying today to migrate my company’s app from v3 to v4, and I’m facing a problem with eventSources component. In v3, we used beforeSend function to get http request, and set Authorization header (for bearer token).

Now in v4, it does not call beforeSend function, and does not use headers params. I didn’t find any doc or code from the internet with a solution.

Is it just not implemented ?

We’ve search in source and found nothing about it too.

What I tried :

(v3 base code)

beforeSend: function (xhr) {
    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},

(v4)

headers: { 'Authorization': 'Bearer ' + token }
headers: { Authorization: 'Bearer ' + token }

Thanks in advance !

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
m8iogcommented, Feb 13, 2020

I don’t know if you’ve solved your problem @azuken, but I found a solution in the event source as a function.

                events: function (info, successCallback, failureCallback) {
                    let start = moment(info.start.valueOf()).format('YYYY-MM-DD');
                    let end = moment(info.end.valueOf()).format('YYYY-MM-DD');
                    $.ajax({
                        url: "api/assignments/?event_id=" + vm.eventId +  '&start='+ start + "&end=" + end,
                        type: 'GET',
                        headers: {
                            'X-CSRF-TOKEN': window.csrf_token
                        }, success: function (response) {
                                successCallback(response);
                        }
                    });
                },

I also used the same approach for the resources, which works for me. It’s not the most elegant way, but it gets the job done.

Anyhow, just thought I’d share my solution.

7reactions
arshawcommented, Apr 22, 2019

in v4, we should offer a hook to manipulate the XHR before it requests. keeping this ticket open for this feature request

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to modify XMLHttpRequest data from beforeSend ...
Yes you can modify it, the signature of beforeSend is actually (in jQuery 1.4+): beforeSend(XMLHttpRequest, settings).
Read more >
XMLHttpRequest - Web APIs - MDN Web Docs
Chrome Edge XMLHttpRequest Full support. Chrome1. Toggle history Full support. Edge12... XMLHttpRequest() constructor Full support. Chrome1. Toggle history Full support. Edge12... abort Full support. Chrome1. Toggle...
Read more >
jQuery.ajax() | jQuery API Documentation
A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent.
Read more >
jQuery AJAX POST Tutorial - AirPair
Just as the HTML page from our mental model above was a single GET request, we could do the same with other types...
Read more >
Data Fetching Techniques with React - OpenReplay Blog
We will focus on using Hooks introduced in React 16.8 to fetch data from the popular JSON Placeholder API to be displayed in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found