【问题标题】:How do I send PHP POST and FILES data to an AJAX URL call when they are not from an HTML form?当 PHP POST 和 FILES 数据不是来自 HTML 表单时,如何将它们发送到 AJAX URL 调用?
【发布时间】:2017-09-20 19:49:07
【问题描述】:

我有一个 PHP 页面,其中填写了 $_POST$_FILES 数据。POST 数据不是从传统的 HTML 表单中获取的,而是在多个页面之间获取的。

例如,POST数据是一些东西:

$_POST["username"]
$_POST["password"]
$_FILES
etc.

问题是,如何将此 POST 数据发送到页面内的 AJAX 调用?

我知道表格是这样的:

$.ajax({
   type: 'post',
   url: 'post.php',
   data: $('form').serialize(),
   success: function () {
     alert('form was submitted');
   }
 });

但是直接来自 PHP 的 POST 数据呢?

【问题讨论】:

  • 您是否尝试将$_POST 数据从php 发送到客户端?
  • 我对你想要做什么感到困惑。 AJAX 是调用服务器端的客户端概念。不是反过来。您不能在 PHP 代码中使用 Ajax 服务器端。
  • 您想将数据返回到 JavaScript 吗?
  • 对不起,我对帖子进行了轻微的编辑。 POST 数据被传递到第二个 PHP 页面,该页面加载并通过 Ajax 调用自动启动实时进度条。我可以从 cmets 看出我可能设计的不正确。
  • 这样做:data: { userid: "joe smoe", firstName:"joe"} 你可以将你的值作为参数传递 key: value...

标签: javascript php jquery ajax post


【解决方案1】:

问题是,如何将此 POST 数据发送到 AJAX 调用 在页面内?

您可以使用WebSocketEventSource 以及可能的ServiceWorker 将数据从服务器流式传输到客户端。例如,参见How to read and echo file size of uploaded file being written at server in real time without blocking at both server and client?,其中要求将上传文件的字节进度通过php实时流式传输到服务器上的文件而不会阻塞。

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多