dongshijiao6890 2019-01-27 14:44 采纳率: 0%
浏览 168
已采纳

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

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>   
  • 写回答

1条回答 默认 最新

  • duancu4731 2019-01-27 14:59
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包