noo*_*gui 11 javascript xml ajax http xmlhttprequest
如何正确格式化?
var params = {
"range":"Sheet1!A4:C4",
"majorDimension": "ROWS",
"values": [
["Hello World","123", "456"]
],
}
Run Code Online (Sandbox Code Playgroud)
然后使用POST发送它,如:
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
xhr.onload = requestComplete;
xhr.send(params);
Run Code Online (Sandbox Code Playgroud)
我知道我会遇到错误,因为有一种格式化我的"请求体"的正确方法.它看起来像是数组和JSON的混合,所以我请求你的帮助如何正确格式化它.
Vla*_*nut 18
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onload = requestComplete;
xhr.send(JSON.stringify(params));
Run Code Online (Sandbox Code Playgroud)
你试过了吗?你不能只是假设你会遇到错误。除非你尝试,否则你不会知道。尝试第一种方法,如果失败,您会发现一种行不通的方法。然后你会找到其他可行的方法,这就是我们学习的方式。我们是从错误和失败中学到的,而不是从成功中学到的。
话虽如此,如果您的方法失败了,请尝试JSON.stringify在发送之前使用参数,就像这样
xhr.send(JSON.stringify(params))
Run Code Online (Sandbox Code Playgroud)
那应该工作。