PHP文件中获取数据返回空值
P粉617237727
P粉617237727 2023-08-30 11:20:14
0
1
339
<p>我正在尝试测试和学习 fetch 的工作原理,所以我想将一些数据从 javascript 发送到 php 文件,我的问题是我无法检索数据(在 php 文件中),只能得到 null回应。</p> <p>我正在使用 POST,但我尝试使用 GET 得到了相同的结果</p> <p>作为旁注,在控制台中,网络中的连接仅在我按 F5 后出现,据我了解,当我打开 Test.php 时应该已经开始工作。</p> <p><em><strong>datosJS.js</strong></em></p> <pre class="brush:php;toolbar:false;">let datos = { method: &quot;POST&quot;, headers: { &quot;Content-type&quot; : &quot;application/json&quot; }, body: JSON.stringify({ username:&quot;Jonathan&quot;, email:&quot;jonathan@gmail.com&quot;, password:&quot;123456&quot; }) } fetch('Test.php',datos) .then(resp =&gt; resp.text()) .then(resp =&gt;{ console.log(resp); })</pre> <p><em><strong>Test.php</strong></em></p> <pre class="brush:php;toolbar:false;">&lt;script src=&quot;datosJS.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; //I use this to call &quot;datosJS.js&quot; to receive the data here in Test.php &lt;?php $body = json_decode(file_get_contents(&quot;php://input&quot;), true); var_dump($body); ?&gt;</pre> <p>我知道 PHP 在服务器端,JS 在客户端,并且它们都在不同的时间运行,但是我很难找到这个问题的解决方案。</p> <p>感谢您的帮助!</p> <p>** 编辑了 fetch 中的一个小更改,现在让我可以在控制台中查看数据</p>
P粉617237727
P粉617237727

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!