swoole, http \ server machine experiments across domains --- remember the last time php website cross-domain access

Reason: For excellent coroutine Mysql client a better experience swoole components, to achieve better concurrent design; to write a small program.

Preparing the environment:

Not using any frame except for using smarty templates to render the data in response to the rear end php, implemented following a html file

  • html part
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
<meta name="author"
    content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.5">
<title>老板控制台</title>

<link rel="canonical"
    href="https://v4ing.bootcss.com/docs/4.3/examples/dashboard/">

<!-- Bootstrap core CSS -->
<link href="http://staticsrc.cn:8888/bootstrap.css" rel="stylesheet">


......Some codes are omitted html main"

            <= main Role" class="col-md-9 ml-sm-auto col-lg-10 px-4">
                <div
                    class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3">
                    <h1 class="h2">添加新订单项目</h1><span id="hintword"></span>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group mr-2">
                            <button type="button" class="btn btn-sm btn-outline-secondary">Share</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary">Export</button>
                        </div>
                        <button type="button"
                            class="btn btn-sm btn-outline-secondary dropdown-toggle">
                            <span data-feather="calendar"></span> This week
                        </button>
                    </div>
                </div>
                <form>
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label for="item">项目编码(字母)</label>
                            <input type="text" class="form-control" id="colname" placeholder="" value="" required>
                            <div class="invalid-feedback">
                                Valid first name is required.
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label for="colname">Project Name (Chinese description) </ label>
                            <input type="text" class="form-control" id="comment" placeholder="" value="" required>
                            <div class="invalid-feedback">
                                Valid last name is required.
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label for="comment">明细分类</label>
                            <select class="custom-select d-block w-100" id="mxname" required>
                                <option value="incomedoc_mx">明细</option>
                                <option value="incomedoc">表头</option>
                            </select>
                            <div class="invalid-feedback">
                                Please select a valid country.
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label for="mxbz">分类标记</label>
                            <select class="custom-select d-block w-100" id="mxbz" required>
                                <option value="s">明细从表</option>
                                <option value="m">明细主表</option>
                                <option value="h">表头</option>
                            </select>
                            <div class="invalid-feedback">
                                Please provide a valid state.
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-8 mb-3">
                            <label for="state"></label>
                            <button class="btn btn-primary btn-lg btn-block" onclick="postclick();" type="button">提交</button>
                        </div>
                    </div>
                </form>
<!--                 <canvas class="my-4 w-100" id="myChart" width="900" height="380"></canvas>
 -->
            </main>
        </div>
    </div>
    <script src="http://staticsrc.cn:8888/jquery-3.4.1.min.js"></script>
    <script src="../../jsconfig.js"></script>
    <script>
        function postclick() {
            $.getJSON("http://127.0.0.1:9501?jsoncallback=?", function(data) {

                $('#hintword').html(data);
            });
        }

    </script>
</body>
</html>

After planned clicks the submit button, the function jquery postclick () to submit a request to a .php file processing, the logic implementation is as follows php file (pseudo code):

<? PHP 
 Go (function () { 
    // 'several logic implemented .......' 
    echo $ request-> GET [ ' jsoncallback ' ]
     // 'several logic implemented .......' 
}) ;
 ?>

But, unfortunately ....... browser series of prompts 500 errors, but to perform a separate php file, no error, it seems swoole library can not simply embed html code by such conventional techniques

  • The right solution
By a small " show " inspired by a friend of (he Swoole \ Http \ Server implements mysql connection pool), but I desire to achieve desires intolerable, so ready to try Swoole \ Http \ Server package Mysql client coroutines treatment options
<?php
require('../../../config.php');
use Swoole\Coroutine as co;
//echo $_POST['colname'];
$result='';
$http = new Swoole\Http\Server("http://ryanbackdb.com", 9501);
$http->on('request', function ($request, $response) {
    //var_dump($request);
    $response->header('Access-Control-Allow-Origin', '*'); # Line is particularly important, the server must allow cross-domain access
     $ Response -> header ( ' the Content-the Type ' , ' file application / JSON ' ); 
    $ jsoncallback = htmlspecialchars ($ request-> GET [ ' jsoncallback ' ]); # jquery acquisition section of the cross-domain route jsoncallback 
    go (function () use ($ response, $ jsoncallback) {# logic portions may need to prepare actual traffic 
        $ json_data = ' [ "CustomerName1", "customername2"] ' ;
         // Response- $> Write (the callback $ _ "(" $ Data ")."..); 
        $ Response-> Write ($ jsoncallback. "(" . $json_data . ") " ); Return result after treatment # 
        $ Response -> End (); 
    }); 
}); 
$ HTTP -> Start ();

Written after the run, yes, this way, in 9501 the port will run a Swoole \ http \ server server, we can continue to enrich the functions according to actual needs, for example according to different routes, different co-writing process is complete different functions, of course, such an action would violate the same-origin policy, we need to deal with cross-domain

 

Guess you like

Origin www.cnblogs.com/saintdingspage/p/11655922.html