【问题标题】:Adding Accept header 'text/xml' in JQuery ajax file upload在 JQuery ajax 文件上传中添加 Accept header 'text/xml'
【发布时间】:2016-06-15 06:15:57
【问题描述】:

如何在 JQuery ajax 文件上传中添加接受标头“text/xml”? 它将上传一个 PDF 文件。

这是我正在使用的代码:

var fd = new FormData(document.getElementById('file'));
$.ajax({
  url: '/myurlhere',
  type: 'POST',
  data: fd,
  cache: false,
  contentType: false,
  processData: false,
  success: function(response) {
    console.log('success... '+response);
  }
}).fail(function(xhr,status,error) {
  console.log(error);
});

服务器期望接受标头是 xml,因此会导致 406 错误。 我尝试过使用 dataType: 'xml' 或 header { Accept: 'text/xml' },它会导致 400 错误。

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    您必须将contentType 设置为"text/xml"

    像这样改变你的电话:

    var fd = new FormData(document.getElementById('file'));
    $.ajax({
      url: '/myurlhere',
      type: 'POST',
      data: fd,
      cache: false,
      contentType: "text/xml",
      dataType: "text",
      processData: false,
      success: function(response) {
        console.log('success... '+response);
      }
    }).fail(function(xhr,status,error) {
      console.log(error);
    });
    

    查看这个 SO 答案可能会有所帮助

    jQuery ajax post to web service

    【讨论】:

    • 嗨,我已经尝试过 contentTYpe: 'text/xml' 和 dataType: 'text',但我收到 406 Not Acceptable 错误,我检查了开发人员工具,它显示 Accept 标头为文本/plain,这可能是它不工作的原因,因为服务器接受 text/xml。我尝试了 dataType: 'xml' 并收到 400 错误。还有其他想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多