简体   繁体   English

在PHP中使用create-react-app

[英]Use create-react-app with php

I'm new to react and want to use it with index.php. 我是新来的反应者,想与index.php一起使用。 I made my react project using create-react-app. 我使用create-react-app创建了我的React项目。 I researched about it and found that you can use react with php after building it. 我对此进行了研究,发现您可以在构建它之后使用php进行反应。 But I want to work with development mode. 但是我想使用开发模式。 Please help me with this. 请帮我解决一下这个。

use another server for PHP; 为PHP使用另一台服务器;

first debug React, "npm start" http://localhost:3000/ 第一次调试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;
} 

sec debug php whis IIS http://my-app.local/ sec调试php以及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 vs代码 边缘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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