当try 使用简写语法在使用jQuery的AJX请求中通过文件上传发送表单数据时,我遇到了"非法调用"错误.然而,当我使用longer .ajax()方法时,错误就会消失.我使用CodeIgniter作为服务器端框架.

简写语法出现错误的原因是什么?为什么.ajax()方法有效?

形式

<形式 id="add_category_形式" action="<?= base_url("CategoriesController/process_add_category") ?>" method="post" enctype="multipart/形式-data">
    <input type="hidden" name="<?= $this->security->get_csrf_token_name() ?>" value="<?= $this->security->get_csrf_hash() ?>" />
    <h2>Add a Category</h2>
    <ul>
        <li>
            <input type="text" name="name" required>
            <label>Category Name</label>
        </li>
        <li>
            <textarea name="description" required></textarea>
            <label>Description</label>
        </li>
        <label>Upload Images (5 Max)</label>
        <input type="file" name="image" id="category_image" accept="image/*">
    </ul>
    <!-- FIXME: center the content of this last li tag -->
    <button type="button" data-dismiss="modal" aria-label="Close">Cancel</button>
    <button type="submit">Save</button>
</形式>

使用$.Post

$(document).ready(function() {
  $("#add_category_形式").submit(function(e) {
    e.preventDefault();
    let url = $(this).attr("action");

    if ($("#category_image").val() == "") {
      alert("Please add an image");
    } else {
      let 形式Data = new FormData(this); // Pass the 形式 element

      $.post(url, 形式Data, function(response) {
        console.log(response);
        csrfName = response.csrfName;
        $("input[name='<?= $this->security->get_csrf_token_name() ?>']").val(response.newCsrfToken);
      }, "json");
    }
    return false;

使用$.ajax

$(document).ready(function() {
  $("#add_category_形式").submit(function(e) {
    e.preventDefault();
    if ($("#category_image").val() == "") {
      alert("Please add an image");
    }

    let 形式Data = new FormData(this);
    $.ajax({
      url: "<?= base_url(''); ?>CategoriesController/process_add_category",
      type: "POST",
      data: 形式Data,
      processData: false,
      contentType: false,
      dataType: "json",
      success: function(response) {
        console.log(response);
        csrfName = response.csrfName;
        $("input[name='<?= $this->security->get_csrf_token_name() ?>']").val(response.newCsrfToken);
      },
      error: function(xhr, status, error) {
        console.error("AJAX Error:", status, error);
      }
    });

    return false;
  });
});

推荐答案

当使用$.post()的简写版本时,您不能将FormData对象用于数据.documentation表示第二个参数必须是普通对象或字符串,而不是FormData.

您需要使用类似于$.ajax()的设置对象,以便可以指定processData: false.如果没有此选项,它会try 将对象转换为URL编码的字符串.

Php相关问答推荐

如何在WordPress中为特定自定义帖子类型自定义URL struct

管理员订单列表中的自定义列与WooCommerce 8.6.1一起消失

在ShipStation for WooCommerce中使用自定义订单项目元数据

PHP-转义字符串内的双反斜杠

WooCommerce基于元数据的自定义范围目录排序

PHP日期操作,将多个日期输出为格式化字符串

从订单项目中获取ACF WooCommerce产品价值

将一个情态形式放在细丝v3中的形式中间

SendGrid响应PHP

如何在UML类图中可视化变量引用数组

Rappasoft Datatables V2(我想在另一个表具有相同值列时显示数据)

强制客户在注册时 Select WooCommerce角色

在 WooCommerce 中何时何地执行 woocommerce_order_status_completed_notification 挂钩?

Woocommerce 临时购物车税未根据第一个请求正确计算

我不明白为什么我收到未定义的数组键异常错误 - Laravel 9 PHP 8

使用 wp_footer 挂钩添加一些 CSS 不起作用

PHP简单地将物品装在盒子里会导致未使用的空间,但还有剩余容量

自定义主键值未正确显示

计算组合指数

来自命令行的 PHP 没有给出正确的结果