使用提取获取消息正文



在jquery中,我可以这样做:

$.ajax({
url: "my_sharepoint_site",
type: "post",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
},
success: function(data){
var m = data.d.GetContextWebInformation.FormDigestValue;
}
});

我正在尝试使用 fetch 获取相同的响应数据。

fetch("my_sharepoint_site", {
method: "post",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
}
}).then(function(response){
// response doesn't contain 'd'
});

我似乎无法弄清楚如何获取其余数据。我敢肯定,这是一件简单得离谱的事情。

fetch

不会自动解析JSON响应。您必须明确地这样做。

参见 MDN 上的示例;

fetch("my_sharepoint_site", {
method: "post",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
}
}).then(function (response) {
return response.json();
}).then(function(data) {
// data should contain 'd'
});

您需要探索.json()方法:

fetch("my_sharepoint_site", {
method: "post",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
}
})
.then(response => response.json())
.then(data => console.log(data));

最新更新