When running Axios in V8Js all requests fail as XMLHttpRequest is not available.
How can we make axios request work server side?
When running Axios in V8Js all requests fail as XMLHttpRequest is not available.
How can we make axios request work server side?
Copyright © 2021 Jogjafile Inc.
It's possible to bind PHP functions or classes to JavaScript with V8Js.
For example in PHP you can:
When executed this will produce the string 'Hello Bob'.
So, know this we can create the XMLHttpRequest in PHP and then bind it to JS in the same way.
First we need to create the XMLHttpRequest class to actually make the requests. For this I'm using Guzzle.
Then after creating a V8Js instance we can attach the new library:
Now we need to tell Axios to use our library. We can do this by first creating an adapter:
And then adding an interceptor to set the adapter and the baseURL:
After this using a normal
axios.postwill work server side.