0Day Forums
unable to access the prestashop webservice via getJSON - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+---- Forum: PrestaShop (https://zeroday.vip/Forum-PrestaShop)
+---- Thread: unable to access the prestashop webservice via getJSON (/Thread-unable-to-access-the-prestashop-webservice-via-getJSON)



unable to access the prestashop webservice via getJSON - uningratiating978901 - 07-27-2023

I am trying to use the webservice prestashop (1.6) to get my product by using the jquery function getJSON() but on the console's browser, I get the following error :

XMLHttpRequest cannot load

[To see links please register here]

.
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost' is therefore not allowed access.

I tried to add " Header set Access-Control-Allow-Origin: * " to the htaccess but without success.

I wonder if there is another way to set a header (except using php with `header('Access-Control-Allow-Origin: *');` )




RE: unable to access the prestashop webservice via getJSON - peneplanes939003 - 07-27-2023

I solved the issue by myself, I just add `header('Access-Control-Allow-Origin: *');` on the file dispatcher.php in the webservice folder


RE: unable to access the prestashop webservice via getJSON - alaster821 - 07-27-2023

In prestashop 1.6 you can try to add this in

./prestafolder/webservice/dispatcher.php

for security reason, instead of asterisk you can type domain name

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Origin:

[To see links please register here]

');

FYI

If you will still have problems or `Unauthorized` message, try to change your url from

[To see links please register here]


into

[To see links please register here]




RE: unable to access the prestashop webservice via getJSON - ectomeres568679 - 07-27-2023

for me i tried with react app i had to made some modification in dispatcher.php and add for option preflight 200 return

i added in the dispatcher.php header

//to access from external browser
header('Access-Control-Allow-Origin: *');
header( 'Access-Control-Allow-Headers: Authorization, Access-Control-Allow-Headers,
Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-
Control-Request-Headers,Output-Format');
header( 'Access-Control-Allow-Methods: GET, OPTIONS, HEAD, PUT, DELETE');
header( 'Access-Control-Allow-Credentials: true');

then modified like this after in the code


if ($method === 'OPTIONS') {
die('200');
ob_end_flush();
}else{
if (isset($_SERVER['PHP_AUTH_USER'])) {
....
}


RE: unable to access the prestashop webservice via getJSON - kemodrlalldb - 07-27-2023

If you are working with Angular or Similar Frameworks, modify and add the following code according to your environment on the first line of `prestashop_folder/webservice/dispatcher.php` file

// Allow from any origin
if(isset($_SERVER["HTTP_ORIGIN"]))
{
// You can decide if the origin in $_SERVER['HTTP_ORIGIN'] is something you want to allow, or as we do here, just allow all
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
}
else
{
//No HTTP_ORIGIN set, so we allow any. You can disallow if needed here
header("Access-Control-Allow-Origin: *");
}

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 600"); // cache for 10 minutes

if($_SERVER["REQUEST_METHOD"] == "OPTIONS")
{
if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_METHOD"]))
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT"); //Make sure you remove those you do not want to support

if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

//Just exit with 200 OK with the above headers for OPTIONS method
exit(0);
}