简体   繁体   中英

getting error of Access-Control-Allow-Origin header

i am trying paypal payment gatway in my website. on form submition its show error in console log

Access to XMLHttpRequest at 'https://www.sandbox.paypal.com/cgi-bin/webscr' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

i have alredy added header codes on files.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, 
Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");

and also added in apache server.

Header set Access-Control-Allow-Origin "*"

but showing error. please help!

here is my originating script.

<form action="<?php echo $paypalURL; ?>" method="post">

                        <span>What service do yon need?</span><br>
                        <select name="item_name" id="service" class="select"> 
                            <option value="cleaning" data-id='5' data-no='1'>cleaning services at home</option>
                            <option value="parchase_grossery" data-id='5' data-no='2'>purchase of grossery</option>

                        </select><br>
                        <span>How many staff do you need?</span><br>
                        <select id="staff" class="select"> 
                            <option  data-id='1'>1</option>

                        </select><br>
                        <span>For how much Hour</span><br>
                        <select id="hour" class="select">
                            <option value="1">1 hour</option>
                            <option value="2">2 hour</option>

                        </select><br>
                         <span>You have to PAY:-</span> <br>
                         <input id="amount" type="text" name="amount" value="0" disabled><br>
                        <textarea>Enter address where you need these services</textarea><br>
                            <p>by clicking on button your are agree to our terms and conditions.</p><br>
                        <!-- Identify your business so that you can collect the payments. -->
                        <input type="hidden" name="business" value="<?php echo $paypalID; ?>">

                        <!-- Specify a Buy Now button. -->
                        <input type="hidden" name="cmd" value="_xclick">

                        <input type="hidden" name="item_number" value="">
                        <input type="hidden" name="currency_code" value="USD">

                        <!-- Specify URLs -->
                        <input type='hidden' name='cancel_return' value='http://localhost/paypal/cancel.php'>
                        <input type='hidden' name='return' value='http://localhost/paypal/success.php'>
                        <!-- Display the payment button. -->
                        <input type="image" name="submit" border="0"
                        src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
                        <img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
                    </form>   

It's just code example for your help. So that you can send request to another server using CURL. Here $posted_data will be all value in a array.

APIURL replace with your url " https://www.sandbox.paypal.com/cgi-bin/webscr "

Hope you will get how to solve your problem

    try {
        $headers = array(
            'Content-Type: application/json',
        );
        $ch = curl_init();
        $options = array(
            CURLOPT_URL => 'APIURL',
            CURLOPT_HEADER => false,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => json_encode($posted_data),
            CURLOPT_FAILONERROR => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SAFE_UPLOAD => false,
            CURLOPT_SSL_VERIFYPEER => false,
        );
        curl_setopt_array($ch, $options);
        $jsonResponse = curl_exec($ch);
        curl_close($ch);

} catch (\Exception $ex) {
    $result['status'] = 0;
    $result['message'] = 'error';
    $result['debug'] = $ex->getMessage();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM