正在发送 HTTP 请求以调用 Google Sheet API 来编辑单元格

Sending HTTP Request to call Google Sheet API to edit a cell

我想制作一个网页,从 URL 查询中获取数据并将它们写入 Google 传播 sheet。我尝试的是在我的 HTML 代码中制作 Atom.xml 和 link 并使用

     XMLHttpRequest().setRequestHeader("Content-type","application/atom+xml")

在我的 JavaScript 代码中。

我的问题很简单,我应该如何发送这个带有 Google 要求我在这个 PUT 请求的 'body' 中包含的 <entry>...</entry> 的请求?

看了Sheet API 文档,在Google 上找了半天也没有结果。我不知道如何为 PUT 请求设置 'body'。

我该怎么做?

我不熟悉 XMLHttpRequest。我承认这不是您要找的答案;但是,我认为这对可能偶然发现此问题的任何人都有用,并且可能对您有用。

人们通常在他们的网站上使用框架,例如 jquery 或 angularjs。

https://jquery.com/ 在 jquery 中,您可以使用如下正文执行放置请求:

var xmldata = '<some xml... >';

$.ajax({
    method: "PUT",
    url: "some_url",
    contentType: 'application/atom+xml'
    data: xmldata
})
.done(function( msg ) {
    alert( "Data Saved: " + msg );
})
.fail(function(e){
    console.log("There was a problem!");
    console.log(e);
})
.always(function(d) {
    console.log("This will always happen!");
});

http://api.jquery.com/jquery.ajax/

考虑使用这样的工具,因为它可以使您的代码更简单、更易于阅读。最重要的是:它可以节省您的时间和精力。

编辑详细说明promise的回调和参数:

jqXHR.done(function( data, textStatus, jqXHR ) {}); An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details.

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {}); An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { }); An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.

发件人:http://api.jquery.com/jquery.ajax/