SharePoint How to return item field value by Ajax

Sevil Red 101 Reputation points
2020-08-19T13:10:50.697+00:00

Good day,

I have the List. The List has checkbox field "DocOrg". The user must select checkbox "DocOrg" value and the script must send ajax query to another list, find another list element by "DocOrg" value and get its field "OrgIndex" value. Then "OrgIndex" value I must add to document "Title" field value. The ajax request works fine. But I can't get return value from getDocOrganization() function. The var orgIndex is equal "". I try to change form Title field value inside the getDocOrganization() function, but this change doesn't save after document saving. How can I get "OrgIndex" value by ajax request, add it to document "Title" field and save this field value in document? When I use "alert" it works fine, but if I don't use alert, it doesn't work.
My script in Attachment
18795-forquestion.txt

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,371 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,619 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,036 Reputation points
    2020-08-20T04:47:42.693+00:00

    By default, all ajax requests are sent asynchronously. The getDocOrganization() function returns before the AJAX call returns.

    As a workaround, you could make the AJAX call synchronous with the async: false option.

    Like this:

    $.ajax({    
        async: false,
        url: url + "/_api/web/lists('3224B8D6-EE69-442C-818B-2BD2224C481C')/items?$filter=Title eq '" + encodeURIComponent(docOrg) + "'",
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.