首页 > 解决方案 > 使用 fetch 获取消息正文

问题描述

在 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'

});

我似乎无法弄清楚如何获取其余数据。我敢肯定这很简单。

标签: javascript

解决方案


您需要探索该.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));

推荐阅读