1

我的数据是一个 xml 元素,我想用 JavaScript 发送 PUT 请求。我该怎么做呢 ?

供参考:更新单元格

编辑: 按照弗雷德里克的建议,我这样做了:

<form id="submitForm" method="PUT" enctype="application/atom+xml" action="https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1">
            <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
                <id>https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1</id>
                <link rel='edit' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1'/>
                <gs:cell row='2' col='1' inputValue='300'/>
            </entry>
            <input type="submit" value="submit"/>
        </form>

但是,它不会回写,而是肯定会返回 xml 文件,例如:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006' xmlns:batch='http://schemas.google.com/gdata/batch'>
    <id>https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1</id>
    <updated>2011-01-11T07:35:09.767Z</updated>
    <category scheme='http://schemas.google.com/spreadsheets/2006' term='http://schemas.google.com/spreadsheets/2006#cell'/>
    <title type='text'>A2</title>
    <content type='text'></content>
    <link rel='self' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1'/>
    <link rel='edit' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1/1ekg'/>
    <gs:cell row='2' col='1' inputValue=''></gs:cell>
</entry>

在这里, inputvalue 是空的!但是,在我的 xml 字符串中是 300。任何进一步的解决方案?

4

1 回答 1

1

由于 HTTP 协议仅支持发送字符串,我不确定您是否能够做到这一点。但是您可以尝试使用jQuery 的 ajax 方法并将方法更改为 PUT 和内容类型并发送序列化的 XML。

jQuery 文档说:

要发出的请求类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他 HTTP 请求方法,例如 PUT 和 DELETE,但并非所有浏览器都支持。

调用 ajax 调用:

 $.ajax({
  url: 'ajax/test.html',
  type: 'PUT',
  contentType: 'text/xml',
  processData: false,
  data: xmlDocument,
  success: function(data) {
    console.log(data);
  }
});

希望它有效。

编辑:请提供有关您要执行的操作的更多信息/代码。

于 2011-01-12T13:37:34.090 回答