【问题标题】:Use create-react-app with php将 create-react-app 与 php 一起使用
【发布时间】:2019-01-30 02:00:58
【问题描述】:

我是新手,想将它与 index.php 一起使用。我使用 create-react-app 制作了我的 react 项目。我对其进行了研究,发现您可以在构建后使用 react 和 php。但我想使用开发模式。请帮我解决这个问题。

【问题讨论】:

    标签: php reactjs integration create-react-app


    【解决方案1】:

    为 PHP 使用另一个服务器;

    第一次调试 React,“npm start”http://localhost:3000/

    let buttonPHPSet = () => document.getElementById("buttonPHP").addEventListener("click", (e) => {
        const that = e.target;
        const data = {
            "p": "7",
            "h": "2",
            "P": "3"
        };
        postAjax("http://my-app.local/controller/HomeController.php", data, (result) => {
            let json = JSON.parse(result);
            that.textContent = JSON.stringify(json);
            console.log(json);
        });
    }, false);
    
    function postAjax(url, object, success) {
        let xhr = new XMLHttpRequest();
        xhr.open('POST', url, true); //URL-aдpec интерпретируется относительно страницы, с которой вызывается код, хотя можно указать и абсолютный путь //только готовит его к отправке
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                success(xhr.responseText);
            } 
        };
        xhr.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
        xhr.send(JSON.stringify(object));
    
        // let xhrFormData = new FormData();
        // for (const key in object) {
        //     if (object.hasOwnProperty(key)) {
        //         xhrFormData.append(key, object[key]);
        //     }
        // }
        // xhr.send(xhrFormData);
    
        // const allHeaders = xhr.getAllResponseHeaders();
        // xhr.abort();
        return xhr;
    } 
    

    秒调试 php whis IIS http://my-app.local/

    <?php 
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");
    header('Content-Type: text/plain; charset=utf-8');
    
    // header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT");
    // header('Access-Control-Allow-Credentials: true');
    // header('Content-Type: application/json;charset=UTF-8');
    // header('Content-type: application/xml');
    // header('Content-type: multipart/form-data);
    // header('Content-type: text/html;charset=utf-8);
    // header('Content-type: application/x-www-form-urlencoded; charset=UTF-8);
    
    $postdata = file_get_contents("php://input");
    $postDataDecode = json_decode($postdata);
    
    if($_POST){
    
        $array = [];
    
        foreach ($_POST as $key => $value) {
            $array[$key] = $value;
        }
    
        echo json_encode($array);
    
    } elseif ($postDataDecode) {
        echo json_encode($postDataDecode);
    }
    
    ?>
    

    vs code edge

    【讨论】:

    • 谢谢。你的回答对我很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-10-26
    • 2021-01-30
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2020-08-14
    • 2020-05-09
    相关资源
    最近更新 更多