بقية إرجاع قائمة العناصر ولكن الرد فارغ في SP2013

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

  •  10-12-2019
  •  | 
  •  

سؤال

أنا محبط جدا ...

لدي وظيفة مسج للاستيلاء على قائمة البيانات وعندما أقوم بتسجيل "البيانات" أرى كامل {d} مع {نتائج} تعليق جميع العناصر القائمة.ولكن عندما أقوم بتشغيله من خلال وظيفة usion $. للحصول على سلسلة يمكنني العودة، عندما أقوم بتسجيل RSP، يقول المصحح "سلسلة فارغة".

هنا هو وظيفتي: giveacodicetagpre.

أنا مجنون وستكون نقدر إلى حد كبير أي نصيحة أو تصحيحات.شكرا مقدما.

هل كانت مفيدة؟

المحلول

$.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