Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 615 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to access the prestashop webservice via getJSON

#1
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: *');` )

Reply

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

#3
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]

Reply

#4
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'])) {
....
}
Reply

#5
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);
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through