【问题标题】:Data method settings for file upload using jquery ajax使用jquery ajax进行文件上传的数据方法设置
【发布时间】:2015-03-08 09:07:51
【问题描述】:

所以这个问题源于对使用 ajax 时数据方法的功能缺乏很好的理解。我在这里查看了大多数关于 ajax 的其他问题,但这部分对我来说并不适用。

例如,我使用 ajax 将表单数据(包括多个文本字段和一个文件上传)发送到 php。然后服务器脚本将字段数据和文件 url 插入到 sql db 中,然后将所有这些值作为 json 编码数组返回。

我当前的 ajax 脚本如下所示,当它运行时,除了文件上传部分外,一切正常。该文件没有保存到服务器,当然也没有 url 回来。如果我改变:

data: data,

data: text,

文件上传工作,文件被保存,但是当 json 数组返回页面时,我得到一个显示数组数据的白屏(它不会返回表单数据来自的页面) .

这是为什么? 什么是正确的数据方法来允许我的文件上传工作和数据返回到它发送的页面。

$("document").ready(function() {
$(".data-form").submit(function() {
        var data = new FormData();
$.each(files, function(key, value)
  { data.append(key, value);
});
    if (confirm("\t\t\tAre you ready to sumbmit this listing?\nYou can always edit the listing after going to the menu tab - edit listing."))       {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "add-list.php",
            data: data,
            processData: false,
            contentType: false,
            success: function(response) {
                      if (response.success) {
                          $("#modal1").modal('hide');
                        $("#add_frame").show();
                        $("#newbutt").show();
                        $(".atitle").html('<a href="' + response.ad_linka + '" target="_blank">' + response.titlea + '</a>');
                        $(".acomment").html(response.commenta);
                        $(".aaddress").html(response.addressa);
                        $(".asale-price").html(response.sale_pricea);
                        $(".alease-price").html(response.lease_pricea);
                        $(".alot-size").html(response.lot_sizea);
                        $(".abuilding-size").html(response.build_sizea);
                        $(".azoning").html(response.zoninga);
                        }
                      else {
                          console.log("An error has ocurred: sentence: " + response.sentence + "error: " + response.error);
                      }
                  },
                  error: function() {
                      alert("An Error has ocurred contacting the server. Please contact your system administrator");
                  }
              });
        return false;
    }
});
});

【问题讨论】:

  • 需要显示你定义datatext的位置
  • 数据 = $(this).serialize();
  • 看一看GOOD看看这个问题stackoverflow.com/questions/166221/…
  • serialize() 不捕获文件,只捕获控件的值。有许多关于如何使用 ajax 上传的教程,以及一些有助于简化上传的好插件
  • 抱歉,看问题和答案

标签: javascript jquery ajax methods


【解决方案1】:

你需要告诉 jquery 停止处理你的数据

     processData: false, // Don't process the files
    contentType: false, // defaults to 'application/x-www-form-urlencoded; charset=UTF-8'

你没有发送你是如何构建数据的,但是这样的东西应该可以工作

    var data = new FormData();
    $.each(files, function(key, value)
      { data.append(key, value);
    });

【讨论】:

  • 所以我使用序列化来定义数据(是的,我应该包含它)。因此,您的更改(我已将其放入原始问题中)允许文件上传,但是页面没有恢复到原始页面,而是转到显示数组的白页。
  • 你如何在 php 中处理你的 POST ?
  • 带有 pdo 和 bindparam。例如 $stmt->bindParam(':title', $_POST['title']);所有数据都回来了(我可以在我的浏览器工具和屏幕上看到它),它只是没有返回到它发送的页面
  • 如果您问数据是如何格式化的?退出(json_encode($response));
  • 对于您需要查看 $_FILES 的文件以及 $_POST 中的其余文件 - 完成后您应该返回 json 结构 - 如果您使用某些框架等,通常您会从控制器返回一个结构/function 如果你只有一个小脚本 echo json_encode({"result" => "ok"}) 或者你需要的任何东西
猜你喜欢
  • 1970-01-01
  • 2011-02-05
  • 2015-09-15
  • 2013-01-05
  • 2017-09-26
  • 2011-06-18
相关资源
最近更新 更多