0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CentOS とOSXでphp, fetch,javascriptの挙動が違う

Last updated at Posted at 2020-06-26

OSXはこれで行けたが、CentOSではダメ

Javascript

function checkNewOrder(uuid) {
            var res = fetch("./test.php", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({
                    //引数が色々あって...。
                })
            }).then((response) => {
                if (response.ok)
                    return response.json(); // レスポンスをテキストとして変換する
            }).then((rcvData) => {
                console.log("rcvData", rcvData); //とりあえず確認
                if (rcvData.length > 0) {
                    let msg = "新たにデータが入りました<br>\n";
                    //rcvData[i]["NAME"]とかを使って色々処理
                }
            });
        }

php

$dbh->beginTransaction();
$stmt = $dbh->query("select * from TableName;");
$stmt->execute();
$dbh->commit();
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
echo json_encode($result, JSON_UNESCAPED_UNICODE);

CentOSだとJSの console.log("rcvData", rcvData);のところで「rcvDataって何?」ってなってしまう。

##結局こうやって対応できた
Javascript

function checkNewOrder(uuid) {
            var res = fetch("./test.php", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({
                    //引数が色々あって...。
                })
            }).then((response) => {
                if (response.ok)
                    return response.text(); // jsonからtextに変更した
            }).then((rcvData) => {
                var rcv = JSON.stringify(rcvData); //JSONオブジェクトに変換
                if (rcv.length > 0) {
                    let msg = "新たにデータが入りました<br>\n";
                    //rcvData[i]["NAME"]とかを使って色々処理
                }
            });
        }

response.json();response.text(); にしてJSON.stringifyでオブジェクト化するとCentOSでOKだった。
OSXとCentOS で挙動が違うのはなぜ?

でも、結局こっちかな

function checkNewOrder(uuid) {
    const response = fetch("./checkNewOrder.php", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            "uuid": ???; //パラメータとか
        })
    }).then((response) => {
        if (response.ok)
            return response.json(); // JSON
       // return response.text(); 
    }).then((response) => {
        console.log("response", response);
        //var rcvData=JSON.parse(response); //result.text()  だったらこっち
        //何らかの処理;
    });
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?