I would use the Http Client and not Ajax, you can then use Ajax to get the results from your own endpoint (or not?)
How can i access an external domain's api using bearer token ?
I'm trying to access another domain's api from my serrver using ajax post request but there's a handicap this request sending by my pc's external ip adress and there's a firewall rule my server's ip adress has been set so when i'm trying to access that api with below code part it throws me an 403 error. Am i using wrong solution by using ajax or is there a better solution for this situation?
$.ajax({
url: 'domain.com',
type: 'POST',
data: {
'user':'user',
'pass':'pass'
},
crossDomain:true,
dataType : 'jsonp',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer Token');
},
success: function (res) {
console.log(JSON.stringify(res));
},
error: function (res) {
console.log(JSON.stringify(res));
},
});
This client-side approach exposes your API credentials publicly; which might have implications with the availability of the service quota.
So, instead consider making an endpoint in your own application that is responding tot he AJAX request; and in that endpoint you control (i) the credentials and (ii) the service usage - through caching for example. Use the Laravel Http Client to make requests to the third party.
Please or to participate in this conversation.