【问题标题】:Sharepoint 2013: Evaluation of ajax GET Requests (XML)Sharepoint 2013:评估 ajax GET 请求 (XML)
【发布时间】:2015-09-24 11:34:37
【问题描述】:

我实际上正在开发一个 SharePoint 2013 应用程序(SharePoint 托管),并且想要获取 SharePoint 列表的项目标题。因此,我使用了 ajax 的 HTTP-GET-function,但现在在评估返回的 XML-Object 时遇到了问题。

这是我的 ajax 请求代码:

var requestURL = appweburl + "/_api/lists/getbytitle('Fragenkatalog')/items?$select=Title";

$.ajax({
        type: "GET",
        url: requestURL,
        dataType: "xml",
        success: function (data) {
            showResult(data);
        },
        error: function (error) {
            console.log("ERROR REST");
        }
    });

在这里得到了我的第一个问题,一开始我尝试从 GET 请求中获取 JSON,因此我将 dataType 更改为 JSON。在我这样做之后,我总是在错误函数中运行。我是否忘记了要添加的内容或为什么它不起作用?

我得到的 XML 文档:

<?xml version="1.0" encoding="UTF-8" ?>
<feed xml:base=http://apps-385225078ec0a6.sp.xyz/_api/ xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
        <id>fd01da84-e9ec-4872-842d-d3d29af08119</id>
        <updated>2015-09-24T09:57:46Z</updated>
        <entry m:etag="&quot;1&quot;">
            <id>Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(1)</id>
            <category term="SP.Data.FragenkatalogListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
            <link rel="edit" href="Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(1)" />
            <updated>2015-09-24T09:57:46Z</updated>
            <author />
            <content type="application/xml">
                <m:properties>
                    <d:Title>Element 1</d:Title>
                </m:properties>
            </content>
        </entry>
        <entry m:etag="&quot;1&quot;">
            <id>Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(2)</id>
            <category term="SP.Data.FragenkatalogListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
            <link rel="edit" href="Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(2)" />
            <updated>2015-09-24T09:57:46Z</updated>
            <author />
            <content type="application/xml">
                <m:properties>
                    <d:Title>Element 2</d:Title>
                </m:properties>
            </content>
        </entry>

    </feed>

这是我的结果函数

function showResult(data) {

    $(data).find('entry').each(function () {
        $(this).find("content").each(function () {
            $(this).find("m:properties").each(function () {
                var Titel = $(this).find("d:Title").text();
                console.log(Titel);
            });
        });
    });
}

现在我想获取列表中每个项目的标题。 但是 .find 在搜索“m:properties”标签时总是失败,这就是为什么我无法获得我正在寻找的信息。

也许你们中的某个人可以帮助我。

感谢和问候

塞巴斯蒂安

【问题讨论】:

    标签: javascript json ajax xml sharepoint-2013


    【解决方案1】:

    在调用 SharePoint REST API 时,您可以通过 Accept 标头请求控制返回数据的格式。下面的例子演示了如何返回 JSON 格式的数据,如下图所示

    示例 1

    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items?$select=Title";
    
    function getJson(requestUrl)
    {
        return $.ajax({
            type: "GET",
            url: requestUrl,
            dataType: "json",
            headers:  { "Accept" : "application/json;odata=verbose" }
        });   
    }
    
    
    getJson(requestUrl)
    .done(function(data){
       //print results
       $.each(data.d.results,function(i,item){
          console.log(item.Title);  
       }); 
    });
    

    示例 2

    Accept 标头被省略时,SharePoint REST 以如下图所示的 JSON 格式返回数据 (minimalmetadata option)

    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items?$select=Title";
    
    $.getJSON(requestUrl,function(data){
       //print results
       $.each(data.value,function(i,item){
          console.log(item.Title);  
       }); 
    });
    

    【讨论】:

      猜你喜欢
      • 2021-02-27
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2020-06-17
      • 2013-01-15
      • 1970-01-01
      相关资源
      最近更新 更多