javascript - axios.interceptors do not fire when fetching with php proxy
Get the solution ↓↓↓I am writing a react app that will consume services on a remote server.
When developing locally, usingreact-scripts
server onlocalhost:3000
, I use a browser with CORS disabled, and axios fetching works just fine: in case of error 400 from a service, for example, my interceptor fires and I can catch the error:
axios.interceptors.response.use(
response => {
return response;
},
err => {
console.error(err); // error handling
}
);
When running in production, I use a custom php proxy, prepending"proxy.php?url="
to the remote url I use for fetching.
The proxy essentially does:
<?php
...
$ch = curl_init($url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
When remote service returns successfully, everything works fine. When remote service fails, for example with a 400 HTTP status code, the axios interceptor does not fire, so I can't react to the error condition in my js code.
Do you see any mistake in my assumptions?
Or any issue in the php proxy?
Or some flaw in my axios interceptor setup?
If I should give more code details I'll do it, of course.
Answer
Solution:
After a lot of fighitng against this issue, I finally found the solution, which was very simple...
My proxy simply did non output the headers coming from server response...
I simply changed
<?php
...
$ch = curl_init($url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
to
<?php
...
$ch = curl_init($url);
$result = curl_exec($ch);
curl_close($ch);
$result = curl_exec($ch);
list($result_headers, $result_body) = explode("\r\n\r\n", $result, 2);
$headers = explode("\n", $result_headers);
foreach ($headers as $h) {
header($h);
}
echo $result_body;
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function getclientoriginalname() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.