简体   繁体   English

fetch 不应该运行 PHP 文件的内容吗? [等候接听]

[英]Isn’t fetch supposed to run the contents of the PHP file? [on hold]

I'm trying to use fetch instead of xmlhttprequest but fetch isn't working the way the comparable ajax system does, or, at least, I can't get it to work the same way.我正在尝试使用 fetch 而不是 xmlhttprequest 但 fetch 的工作方式与可比较的 ajax 系统的工作方式不同,或者至少,我无法让它以相同的方式工作。 I have the following code:我有以下代码:

addAction("pressBTN", "click", function() {
  const data = new FormData();
  data.append('dset1', theElement("dataINP1").value);
  data.append('dset2', theElement("dataINP2").value);
  /* for (var pair of data.entries()) {
  console.log(pair[0]+ ', '+ pair[1]); 
  } */
  fetch('./files/php/scripts/dataEntery.php', {
      method: 'POST',
      body: data
    })
    .then(response =>
      if (!response.ok) {
        throw new Error('Unknown Error #4: nice way to mess it up');
      } else {
        console.log(response);
      }
    }).catch((err) => {
  console.log(err);
});
}, true);

Note that theElement and addAction just keep me from having to type out请注意,theElement 和 addAction 只是让我不必输入

document.getElementById

and

document.addEventListener

over and over.一遍又一遍。

This process returns and ok response.此过程返回并确定响应。 Cool.凉爽的。 Except nothing in the php file gets ran at all.除了 php 文件中的任何内容都没有运行。

With the previous system xmlhttprequest, it called the php file, ran its contents and returned.使用之前的系统 xmlhttprequest,它调用 php 文件,运行其内容并返回。 In that PHP, I could check the values against a database and you know do server stuff.在那个 PHP 中,我可以根据数据库检查值,你知道做服务器的事情。

I've tried moving the php file to the same location as the javascript file.我尝试将 php 文件移动到与 javascript 文件相同的位置。 Nothing.没有什么。 I made sure the data values were formed correctly which is still there in the commented code.我确保数据值的格式正确,它仍然存在于注释代码中。

I've tried json because every tutorial out there shows json.我试过 json,因为那里的每个教程都显示 json。 It still hasn't worked.它仍然没有奏效。 Not even the simplest command like甚至不是最简单的命令

<?php
   print_r($_POST);
   echo “balls!”;
?>

works in the php file.在 php 文件中工作。 I honestly don't know why.老实说,我不知道为什么。 It looks like the code should work to me.看起来代码应该对我有用。 Perhaps I am missing something here.也许我在这里遗漏了一些东西。

Except for the HTML that sets up two inputs as text and a button there is no other code in the project.除了将两个输入设置为文本和一个按钮的 HTML 之外,项目中没有其他代码。 This LOOKS like it shoukd work.这看起来应该有效。 The console returns the following when it logs the response:控制台在记录响应时返回以下内容:

body: ReadableStream
statusText: “OK”
json: (function)
redirected: false
text: (function)
status: 200
bodyUsed: false
url: “goodurlpath/files/php/scripts/dataentry.php”
type: “basic”
blob: (function)
arrayBuffer: (function)
formData: (function)
headers: Headers
value: (function)
keys: (function)
delete: (function)
forEach: (function)
entries: (function)
get: (function)
append: (function)
set: (function)
has: (function)
clone: (function)
ok: true

I'm wondering if the PHP is even supposed to run with fetch??我想知道 PHP 是否应该与 fetch 一起运行?

Any help with why the php isn't running here would be appreciated.任何有关为什么 php 未在此处运行的帮助将不胜感激。

What you have logged above is the Response object.您在上面记录的是Response object。 There is another step required to actually take the stream and read it to completion.实际获取 stream 并阅读完成还需要另一个步骤。

If you update your fetch request to the following it should work:如果您将fetch请求更新为以下内容,它应该可以工作:

fetch('./files/php/scripts/dataEntery.php', {
  method:'POST',
  body: data
}).then(response => {
   return response.text();
}).then(response => {
   console.log(response);
});

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

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