1

I don't understand why my code is running synchronous. At least it is what the attached print-screen shows. Are my assumptions correct?

<script src="../SiteAssets/js/lib/jquery-2.2.0.min.js"></script>

<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="_layouts/15/sp.RequestExecutor.js"></script>

<script>
    var oWeb = "https://xxxxxxxxx.sharepoint.com/sites/edpsaomanoel/";

    $(document).ready(function () {
        getListItems();
        executorGetListItems();
    });

    var endpointUrl = "_api/web/lists/getbytitle('acoes')/items/?$select=Id,Title,DueDate,PercentComplete&$top=200";
    var acceptHeaders = {"Accept": "application/json; odata=minimalmetadata"}

    function onFail (data, errorCode, errorMessage) {
        console.log("Error:",errorMessage,errorCode,data);
    }

    function executorGetListItems() {   

        var executor;
        executor = new SP.RequestExecutor(oWeb);

        executor.executeAsync({
            url: endpointUrl,
            method: "GET",
            headers: acceptHeaders,
            error: onFail,
            success: function (data){
                console.log("ok executor",JSON.parse(data.body).value; //data.body.constructor === String     
                }
        });
    }

    function getListItems() {   

        $.ajax({
            url: oWeb + ""+ endpointUrl,
            method: "GET",
            headers: acceptHeaders,
            error: onFail,
            success: function (data){
                console.log("ok ajax",data.value); //data.value === Object
            }
        });
    }

</script>

enter image description here

0

You are trying to query the same list simultaneously. Try two different lists. If you must query the list twice, return the promise of the ajax request. Then you can do something like:

$.when( getListItems(), executorGetListItems() )
.then(function(dataFromFirstFunction, dataFromSecondFunction){
     //do something
});

Which basically says when both functions are complete, then do something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.