Page 1 of 1

Redirect to external URL

Posted: Mon Oct 09, 2023 8:50 am
by Dev.
Hi All
I'm working on opencart 3. I want to make some changes to the custom plugin.
After the customer selects the payment method I want to redirect to the external page.
In the index function, I try to use
return $this->response->redirect(URL)
But will get the below error.

Code: Select all

Warning: header() expects parameter 3 to be int, string 
In the Console will get the below error.

Code: Select all

index.php:1 Access to XMLHttpRequest at 'URL' (redirected from 'https://URL/index.php?route=checkout/confirm') from origin 'https://URL' 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.
How can solve it?

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 4:48 pm
by JNeuhoff
You haven't told us the whole story here.

The original redirect method from the system/library/response.php class has this signature:

Code: Select all

	public function redirect($url, $status = 302) {
		header('Location: ' . str_replace(array('&', "\n", "\r"), array('&', '', ''), $url), true, $status);
		exit();
	}
Therefore, when it calls the header function it will use the integer value 302 for the 3rd argument, not a string!

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 7:40 pm
by Dev.
JNeuhoff wrote:
Mon Oct 09, 2023 4:48 pm
You haven't told us the whole story here.

The original redirect method from the system/library/response.php class has this signature:

Code: Select all

	public function redirect($url, $status = 302) {
		header('Location: ' . str_replace(array('&', "\n", "\r"), array('&', '', ''), $url), true, $status);
		exit();
	}
Therefore, when it calls the header function it will use the integer value 302 for the 3rd argument, not a string!

Code: Select all

return $this->response->redirect($redirect, '', true);
This is what I do. But still get the same error
I didn't mean what's a missing parameter

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 7:56 pm
by ADD Creative
Try.

Code: Select all

return $this->response->redirect($redirect);

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 8:08 pm
by Dev.
ADD Creative wrote:
Mon Oct 09, 2023 7:56 pm
Try.

Code: Select all

return $this->response->redirect($redirect);
will get error
undefind


and in the console will get

Code: Select all

index.php:1 Access to XMLHttpRequest at 'URL' (redirected from 'https://URL/index.php?route=checkout/confirm') from origin 'https://URL' 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.

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 8:48 pm
by straightlight
Dev. wrote:
Mon Oct 09, 2023 8:08 pm
ADD Creative wrote:
Mon Oct 09, 2023 7:56 pm
Try.

Code: Select all

return $this->response->redirect($redirect);
will get error
undefind


and in the console will get

Code: Select all

index.php:1 Access to XMLHttpRequest at 'URL' (redirected from 'https://URL/index.php?route=checkout/confirm') from origin 'https://URL' 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.
OC version. You're using an XML HTTP request. What is the actual code for the XML request you are using prior with the redirection?

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 8:54 pm
by Dev.
straightlight wrote:
Mon Oct 09, 2023 8:48 pm
Dev. wrote:
Mon Oct 09, 2023 8:08 pm
ADD Creative wrote:
Mon Oct 09, 2023 7:56 pm
Try.

Code: Select all

return $this->response->redirect($redirect);
will get error
undefind


and in the console will get

Code: Select all

index.php:1 Access to XMLHttpRequest at 'URL' (redirected from 'https://URL/index.php?route=checkout/confirm') from origin 'https://URL' 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.
OC version. You're using an XML HTTP request. What is the actual code for the XML request you are using prior with the redirection?
OC 3
I'm writing this in the index function and using curl to post requests. Then will redirect the customer to the external page

Re: Redirect to external URL

Posted: Mon Oct 09, 2023 10:01 pm
by ADD Creative
Dev. wrote:
Mon Oct 09, 2023 8:08 pm
ADD Creative wrote:
Mon Oct 09, 2023 7:56 pm
Try.

Code: Select all

return $this->response->redirect($redirect);
will get error
undefind


and in the console will get

Code: Select all

index.php:1 Access to XMLHttpRequest at 'URL' (redirected from 'https://URL/index.php?route=checkout/confirm') from origin 'https://URL' 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.
Error undefined is probably a JavaScript error. Perhaps you don't want a redirect, but just return the URL and let the JavaScript redirect. You are going to have to give a lot more information for others to understand what you are doing.