简体   繁体   English

SharePoint 365 REST API基本处理-将结果放入div吗?

[英]SharePoint 365 REST API basic handling - put result in div?

Very new to all this. 这一切都很新。

As a learning exercise I'm trying to update a div id "message" on a SharePoint page with a result returned from a REST API call. 作为一项学习练习,我尝试使用REST API调用返回的结果来更新SharePoint页面上的div id“消息”。

I can copy/paste my query url into my browser and get a result so I can't imagine that's the problem, but when I embed the following script I can't seem to get the success or failure to do anything. 我可以将查询URL复制/粘贴到浏览器中并获得结果,所以我无法想象这是问题所在,但是当我嵌入以下脚本时,我似乎无法成功执行任何操作。

Any help would be much appreciated. 任何帮助将非常感激。

<div id="message"></div>

<input type="button" value="Get" onclick="doGet();" >
<script>

function doGet()
  {

var call = jQuery.ajax({
        url: "https://X/sites/forms/_api/web/lists/getByTitle('Y')/items?$select=Z&$top=1",
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });

    call.done(function (data,textStatus, jqXHR) {
    alert("CallDone");
    var message = jQuery("#message");

    jQuery.each(data.d.results, function (index, item) {
    message.append("<br/>");
    message.append(item.Z);
    $get("message").innerHTML = item.Z;

    });
 });
    call.fail(function (jqXHR,textStatus,errorThrown){
    alert("Call failed");            
    alert("Error retrieving: " + errorThrown);  

    });

}


</script>

just make sure that you are making the correct call for the SP REST service: 只需确保对SP REST服务进行了正确的调用:

<button id="button" type="button">Get Data</button>

$('#button').click(function (e) {
  e.preventDefault();
  doGet();
});

function doGet() {
var resultDiv = $('#message');
$.ajax({
    url: "https://X/sites/forms/_api/web/lists/getByTitle('Y')/items?$select=Z&$top=1",
    type: "GET",
    dataType: "json",
    headers: {
        Accept: "application/json;odata=verbose"
    },
    beforeSend: function () {
        console.info("Loading Data");
        resultDiv.empty();
        resultDiv.append(
            $('<p>', { class: 'bg-warning', text: 'Loading....' })
        );
    },
    success: function (data) {
        resultDiv.empty();
        $.each(data.value, function (index, value) {
            resultDiv.append(
                $('<p>', { class: 'bg-warning', text: value.YOURPROPERTY })
            );
        })
    },
    error: function (data) {
        console.error("Error retieving: " + data);
        resultDiv.empty();
        resultDiv.append(
            $('<p>', { class: 'bg-error', text: data })
        );
    }
});

} }

Hope this helps, and if it does, do not forget to mark it as an answer 希望这会有所帮助,如果有帮助,请不要忘记将其标记为答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM