获取Access-Control-Allow-Origin标头错误

时间:2019-01-27 14:44:18

标签: php

我正在我的网站上尝试使用贝宝付款方式。 在表单提交中,其在控制台日志中的显示错误

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.

我已经在文件上添加了标头代码。

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");

,并且已添加到apache服务器中。

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

但显示错误。 请帮忙!

这是我的原始脚本。

<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>   

1 个答案:

答案 0 :(得分:0)

这只是为您提供帮助的代码示例。这样您就可以使用CURL将请求发送到另一台服务器。 $posted_data在这里将是数组中的所有值。

APIURL 替换为您的网址“ https://www.sandbox.paypal.com/cgi-bin/webscr

希望您会找到解决问题的方法

    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();
}