
Javascriptでfetchがうまく動いてくれません。アドバイスいただけないでしょうか。
ローカルのサーバー(php.7.2.24)では動いているのですが、xserverの環境(php.7.2.17)では動いてくれないので、困っております。なお、phpの環境の違いが原因であると断定しているわけではありません。ローカルでは動いてくれるのに、xサーバーにアップしたら動かなくなったというだけです。
phpの側でファイル書き出しする方は確実に動いており、copydata.jsonは正しく出力されているようです。
ですが、javascriptのexecPasteで読み込んだ
document.getElementById(pre1id).value = json.CT_Code;
に対して
TypeError: document.getElementById(...) is null
というエラーが出て、おかしな値がセットされてしまいます。
phpでファイル書き出しするもの
php
1$obj = '{ "CT_Code": "' . $CT_Code . '", "CT_Item": "' . $CT_Item . '", "CT_Kikan": "' . $CT_Kikan . '", "CT_Email": "' . $CT_Email . '" }'; 2$data = json_decode($obj, true); 3 4$filename = 'copydata.json'; 5$somecontent = $obj; 6if (is_writable($filename)=== FALSE) { 7echo "the file is not writable"; 8} 9$json = fopen('copydata.json', 'w'); 10if ( fwrite($json, json_encode($data)) === FALSE) { 11echo "Cannot write to file ($filename)"; 12exit; 13}
Javascriptで読み込むもの
Javascript
1$("#execPaste").click(function () { 2var pre1id = localStorage.getItem('pre1id'); //Storageから 1つ前のフォーカス位置のid読み込み 3var pre2id = localStorage.getItem('pre2id'); //Storageから 2つ前のフォーカス位置のid読み込み 4var pre3id = localStorage.getItem('pre3id'); //Storageから 3つ前のフォーカス位置のid読み込み 5result = pre1id.indexOf("Code"); 6if (result != -1) { 7 pre2id = pre1id.substr(0, result); 8 pre3id = pre1id.substr(0, result) + "MailAddress"; 9} 10 11fetch("copydata.json") 12 .then(response => response.json()) 13 .then(function (json) { 14 document.getElementById(pre1id).value = json.CT_Code; 15 document.getElementById(pre2id).value = json.CT_Item; 16 document.getElementById(pre3id).value = json.CT_Email; 17 }); 18});
1月21日追記します。
以下のようにコンソール出力をさせました。
javascript
1fetch("./copydata.json") 2 .then(response => response.json()) 3 .then(function (json) { 4 console.log(json); 5 document.getElementById(pre1id).value = json.CT_Code; 6 document.getElementById(pre2id).value = json.CT_Item; 7 document.getElementById(pre3id).value = json.CT_Email; 8 });
すると、コンソールには、以下のように書き出されます。
Object { CT_Code: "00029302008", CT_Item: "xxxxxxxxxxxxxxxx", CT_Kikan: "", CT_Email: "" }
これで実は期待どおりに動くのです。
ですが、phpの方でcopydata.jsonが書き換えられても、2回目以降のjavascriptでは、新しいcopydata.jsonの内容が反映されず、最初のデータのままなのです。そして、2回目以降は、
Object { CT_Code: "00029302008", CT_Item: "xxxxxxxxxxxxxxxx", CT_Kikan: "", CT_Email: "" }
TypeError: document.getElementById(...) is null
となります。
ちなみに、
javascript
1$.getJSON("copydata.json", function (data) { 2 //読み込んだdataに対する処理 3 console.log(data); 4 document.getElementById(pre1id).value = data.CT_Code; 5 document.getElementById(pre2id).value = data.CT_Item; 6 document.getElementById(pre3id).value = data.CT_Email; 7});
というものも試しています。
1回目のcopydata.jsonのデータが反映され、エラーメッセージも全く同じ結果になります。
さらに追記します。
2回目のcopydata.jsonが反映されないので、キャッシュを読み込んでいるのかと疑い、キャッシュさせないようにするのと、ctrl+shift+rなどを試してみましたが、依然として最初のcopydata.jsonが反映されるようです。原因が違うのかもしれません。
さらに追記します。
xserverにもキャッシュ設定というものがあるということを発見したのですが、それもoffであることを確認しています。
社内LANのサーバーにアクセスして実行するときちんと動きます。ブラウザをそのまま閉じることなく、別のタブにしてxserverにアクセスすると、とたんに動かなくなります。行ったり来たりしても、社内LANでは動き、xserverでは動きません。
気にかけていただいた方、ありがとうございました。
初心者なので、情報提供もこの程度しか出来ません。






回答3件
あなたの回答
tips
プレビュー