【问题标题】:How can I send XML data using JQuery AJAX as a delete request?如何使用 JQuery AJAX 作为删除请求发送 XML 数据?
【发布时间】:2021-05-06 14:42:14
【问题描述】:

我需要使用 jquery、ajax 作为 DELETE 请求将 XML 类型的数据发送到后端。这会从后端请求正文返回空数组。如何正确发送 id?

这是我的代码,

function deleteProduct(id) {

  var xmlDocument = $(
    `<productsData>
      <Prod_ID>${id}</Prod_ID>
    </productsData>`);

  $.ajax({
    type:"DELETE",
    url:"http://localhost:8000/delete",
    data:JSON.stringify({
      data : xmlDocument
    }),
    contentType: 'application/json',
    dataType: 'text'
  });
}

我需要发送这些数据,

<productsData>
  <Prod_ID>2</Prod_ID>
</productsData>

this 2 来自函数参数。

这是我在 express 中的后端

app.delete('/delete',(req,res,next)=>{
    console.log(req.body);
    res.status(200).json({
        message: "success"
    })
})

这返回空对象。我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery ajax express


    【解决方案1】:

    如果你想发送 XML,不要说你在发送application/json

    function deleteProduct(id) {
      return $.ajax({
        type: "DELETE",
        url: "http://localhost:8000/delete",
        data: `<productsData><Prod_ID>${id}</Prod_ID></productsData>`,
        contentType: 'application/xml'
      });
    }
    

    通过返回 Ajax 请求,您可以执行以下操作:

    deleteProduct(42).done(function () {
      // delete completed, remove e.g. table row...
    }).fail(function (jqXhr, status, error) {
      // delete failed, keep table row & show alert
      alert("Could not delete product: " + error);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      相关资源
      最近更新 更多