I'm trying to do a https request to call a server-hosted PHP file in my react native app, so I tried this way:
fetch('https://XXX.XXX.XX.XXX/m_getApplications.php?offset=0&dateOrder=date_created')
.then((response) => response)
.then((responseJson) => {
console.warn(responseJson);
})
.catch((error) => {
console.error(error);
});
but the only thing I can get is a 404 error page from CPanel.
But the crazy thing is that when I call the SAME link on Postman (http request tool), I'm getting my JSON and everything is working well. I thought that it could be the emulator, but I tried on iOS & Android and both of them are getting the error.
I already set the NSAllowsArbitraryLoads on true but there's no way.
Has anyone encountered the problem already?
Thanks in advance for your help.
Got it! I tried with the domain name instead of the ip address of the server, and it's working now.
Related
I have developed an API project on Laravel with Sanctum (Token) and NextJs for the frontend. I have setup things up correctly and everything is working fine on Localhost.
I deployed the project on Laravel Forge with one custom subdomain (eg. api.example.com). I run php artisan storage:link and php artisan migrate:fresh --seed (with env as staging) as per their guide (cd /to the path && artisan command) and this works. FRONTEND_URL in env has also been updated to the live frontend url (eg. nextjs.example.com).
I tried logging in to the backend from nextjs after deploying Backend on Laravel Forge and NextJs on Vercel. https://api.example.com/sanctum/csrf-cookie is working correctly as it responds to the browser with the XSRF-TOKEN. But it fails on login with csrf-token mismatch.
Then I tried logging into it with Postman and the same thing happens. I can request the csrf-cookie separately but can not log in to the api backend with responded token. However, it is working fine on the localhost.
This is a piece of my working codes on localhost (NextJs)
const csrf = () => axios.get('/sanctum/csrf-cookie');
const login = async (loginDetails, setErrors) => {
setErrors('');
await csrf();
await axios
.post('/login', loginDetails, {
headers: {
'X-XSRF-TOKEN': getCookie('XSRF-TOKEN'),
},
})
.then(async (res) => {
if (res.data.status === 401) {
setErrors(res.data.message);
} else {
localStorage.setItem('user_token', res.data.data.token);
setCookie('user_token', res.data.data.token);
await axios
.get('/api/authenticated-user', {
headers: {
Authorization: `Bearer ${localStorage.getItem('user_token')}`,
},
})
.then((res) => {
localStorage.setItem(
'user_data',
JSON.stringify(res.data.data)
);
});
router.push('/dashboard/);
}
})
.catch((err) => {
setErrors(err.response.data.message);
});
};
I got the solution for this.
The reason it is not working on Postman is that X-Requested-With: XMLHttpRequest & X-XSRF-TOKEN: {{csrf-token}} were missing on the headers after duplicating the one that works on localhost for the live version. If you are experiencing the same issue, please double-check whether those headers are present on the request's headers. Then, it started working on Postman but not on the live NextJs project with the subdomain.
To make it work perfectly on the subdomain, please check this thread since I created a different question at the same time with the purpose of making it clear for the ones who wanted to answer.
Use case: VueJS/Laravel app has inventory. Calling Magento2 using SOAP API to update Qty from VueJS/Laravel.
Error as shared below:
Access to XMLHttpRequest at
'http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx'
from origin 'http://192.168.0.x' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.
Note: I am a beginner in this domain. I might be asking stupid questions. Please bear with me and requesting help here.
Tools/Apps used:
Laravel Framework 7.9.2 /Running on http://192.168.0.x
VueJS /Running on http://192.168.0.x
Magento2 /Running on http://xx.yyy.abc.123
Postman (Tool used to test SOAP API)
Debugging efforts:
URL http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx
Tried from postman: It works, Apended token into postmane and got the response.
Making AXIOS call from VueJS/Laravel application to Magento2:(Failed)
Origin: http://192.168.0.x
Magento2: http://xx.yyy.abc.123
axios.post("http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx",
{
})
.then((response) =>
{
console.log("response.data",response.data);
this.apiResponse = response.data;
//this.getproduct();
})
.catch(error =>
{
alert('ERROR GETCATEGORY!!!! No Data found');
console.log(error.response);
});
Checked for 2 days now about CORS error and found that this is enabled by default in Laravel 7. Not sure why I am seeing this error even after using Laravel 7. There are very few answers or solutions specific to VueJS with Laravel 7.
Question:
What does this error means. I thought it is just calling from webpage1 to webpage2, it does not seem to work.
Do I need to make any changes on my VueJS/Laravel application. Am I missing anything further.
Please do let me know if you need any more information to help me in this regards.
Thanks for your help.
Regards,
I had got the same CORS error while working on a Vue.js project. I finally solved this issue just today in the morning. You can resolve this either by building a proxy server or another way would be to disable the security settings of your browser (eg, CHROME) for accessing cross origin apis. Both these solutions had worked for me. The later solution is the easiest solutoion and does not require any mock server or a proxy server to be build. Both these solutions can be resolved at the front end.
You can disable your browser (CHROME) security settings for accessing apis out of the origin by typing the below command on the terminal:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir="/tmp/chrome_dev_session" --disable-web-security
After running the above command on your terminal, a new chrome window with security settings disabled will open up. Now, run your program (npm run serve / npm run dev) again and this time you will not get any CORS error and would be able to GET request using axios.
Hope this helps!
I'm implementing a payment method in PHP Laravel 5.6, and i'm supposed to receive an online notification through POST to an url i provide. It is getting Error 403, my POSTS from within the APP work completely fine.
As this POST comes from outside with no csrf, i've added my route on VerifyCSRF exceptions. but still it keeps saying 403.
I'm using Laravel 5.6 on AWS Elastic beanstalk on a load balancer, on top of this i use cloudflare.
web.php route
Route::post('/tpv/notificationOK', 'TPVController#order_notificationOK');
VerifyCsrfToken.php
protected $except = [
'tpv/notificationOK',
'https://www.pcmaker.es/tpv/notificationOK'
];
I expect to receive a POST and execute order_notificationOK function on /tpv/notificationOK URL but instead i'm getting "Server returned HTTP response code: 403 for URL: /tpv/notificationOK"
----------EDIT-------------
Just in case it can help someone, Cloudflare was blocking the POST request causing into an error 403, i just had to whitelist the ip where the POST came from and it works fine.
I'm just starting out with api's so pardon me if I'm missing something very obvious. I'm using xampp for my localhost and I've made this api route in Laravel Route::post('/register', 'RegisterController#register');
the name of my app is forum-api
this is the route that I'm using to hit the api through postman (I suspect that the problem is most likely this route):
http://localhost:8080/forum-api.app/api/register
whenever I send a request I get an error saying "Could not get any response"
I had this problem; this is what actually happenedI had typed in the url bar unknownweb.com/api/cars/23/parts/34
INSTEAD OF https://unknownweb.com/api/cars/23/parts/34
I am using Nusoap library in codeigniter. My code works fine in localhost as well as on server before i make new folder for my testing purpose. But now in testing folder it gives error like this.
I'm receiving this Error:
HTTP Error: Unsupported HTTP response status 404 Not Found
(soapclient->response has contents of the response)
I have searched solution Here and Here but it shows that endpoint is not set for client. On my side, when I print my object it has the endpoint value... What do I do wrong?
I can't connect the URL(http://carvilshoe.cz.cc/index.wsdl.php?wsdl) from my web browser too. I think you have to check your WebService status is online.