REST는리스트 항목을 반환하지만 응답은 SP2013에서 비어 있습니다.

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/90325

  •  10-12-2019
  •  | 
  •  

문제

나는 soooo 좌절 ...

목록 데이터를 잡아야 할 jQuery 함수가 있고 "데이터"를 기록 할 때 {d}이 모든 목록 항목을 보유하고있는 {d}이있는 전체 {d}를 볼 수 있습니다.그러나 $ .each 함수를 통해 실행할 때 문자열을 가져 오려면 RSP를 기록 할 때, 디버거는 "빈 문자열"이라고 표시합니다.

여기 내 기능이 있습니다 :

function get_news(){
    var rsp = '';
    $.ajax({
        url: "http://edrdbdev/_api/web/lists/GetByTitle('Latest News')/items",
        type: "GET",
        headers: { "accept" : "application/json;odata=verbose" },
        success: function(data){
            console.log(data);
            $.each(data.d.results, function(index, item){
                rsp +=  "<p>" + item.Title + "</p>"
            });
        },
        error: function(error){
            rsp = JSON.stringify(error);
        }
    });

    console.log(rsp);
    return rsp;
} // end get_props function
.

나는 미치리가되고 조언이나 수정을 크게 감사 할 것입니다.미리 감사드립니다.

도움이 되었습니까?

해결책

$.ajax perform an asynchronous HTTP (Ajax) request. That means sending the request (or rather receiving the response) is taken out of the normal execution flow.

In your example, return rsp is executed before the function you passed as success callback was even called.

Solutions

There are basically two ways how to solve this:

  • Make the AJAX call synchronous.
  • Restructure your code to work properly with callbacks.

Please refer How to return the response from an AJAX call? for a more details

Below is demonstrated the modified version with callback usage:

function get_news(result){
    var rsp = '';
    $.ajax({
        url: "http://edrdbdev/_api/web/lists/GetByTitle('Latest News')/items",
        type: "GET",
        headers: { "accept" : "application/json;odata=verbose" },
        success: function(data){
            console.log(data);
            $.each(data.d.results, function(index, item){
                rsp +=  "<p>" + item.Title + "</p>"
            });
            result(rsp);    
        },
        error: function(error){
            result(JSON.stringify(error));
        }
    });    
} 



//Usage
get_news(function(content){
    console.log(content);
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top