【问题标题】:Server response does not reflect correctly after Ajax postAjax 发布后服务器响应未正确反映
【发布时间】:2014-11-14 01:41:16
【问题描述】:

我正在使用 jquery.iframe-transport.js 通过 IFrame 和 Ajax(使用 Internet Explorer)上传文件 该文件已正确上传,并且服务器会针对每次调用以适当的响应进行响应。但是,回调函数“完成”中的响应“数据”始终包含先前/较旧的响应。即使清除缓存并使用新的浏览器似乎也无济于事。知道必须发生什么吗?

$(function() {
    //alert("Loading");

    getLDAPUsers();
    //getDummyUsers();

    //This will submit the file content using Ajax via Iframe
    $("#myForm").submit(function() {
        alert("Submitting Ajax");
        $.ajax(this.action, {
            data: $(":text", this).serializeArray(),
            files: $(":file", this),
            //iframe: true,
            processData:false
        }).complete(function(data) {
        debugger;
            alert("Response from server ::: "+data.responseText);
        });
    });

【问题讨论】:

标签: javascript jquery ajax iframe


【解决方案1】:

我建议你改用“成功”和“错误”,例如:

$(function() {
    $("#myForm").on('submit',function(event) {
    event.preventDefault();

    //alert("Loading");

    getLDAPUsers();
    //getDummyUsers();

    var request = $.ajax({
      type: 'POST',
      url: $(this).attr('action'),
      data: $(":text", this).serializeArray(),
      files: $(":file", this),
      //iframe: true,
      ifModified: true,
      cache: false,
      success: function (response, textStatus, xhr) {
        alert("Response from server ::: "+response);
      },
      error: function (xhr, textStatus, errorThrown) {
        alert('ERROR');
      }
    });
  });
})();

【讨论】:

  • ...我注意到您的 $('#myForm').submit(function(){});没有停止正常的表单提交,它会像这样完成: $('#myForm').submit(function(e){ e.preventDefault(); $.ajax(this.action, { ... .. . }); });
  • 谢谢你们,但这两种解决方案似乎都不起作用。我指的是这个 - cmlenz.github.io/jquery-iframe-transport
  • 我现在可以让回电正常工作。但是,我看到在执行 Ajax 发布时发送了 2 个 http 请求。一个是不包含任何文件信息的 GET,第二个是 POST,它是带有文件内容的实际请求。回调“完成”正在接收来自 GET 调用的响应,这显然不是我想要的。我无法弄清楚为什么要发送 GET 请求!有什么建议?补充一下,我使用的是 IE9
猜你喜欢
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2016-05-13
相关资源
最近更新 更多