第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

POST 請(qǐng)求循環(huán)遍歷 8 個(gè)不同的 url

POST 請(qǐng)求循環(huán)遍歷 8 個(gè)不同的 url

GCT1015 2023-04-01 16:12:05
我從事一個(gè)項(xiàng)目已經(jīng)有一段時(shí)間了。我發(fā)現(xiàn)這篇文章有些用處,但不確定它是否適合我的使用。功能:使用 GET 請(qǐng)求從 8 個(gè)不同的子網(wǎng)站讀取 SharePoint 列表項(xiàng)。在單個(gè)登錄頁(yè)面上的數(shù)據(jù)表中以有序(分組)方式填充這些項(xiàng)目。DataTable 具有按程序分組的可折疊/可展開(kāi)行,然后是可交付成果。帶有打印/excel/PDF/更新表格按鈕的下拉菜單。更新表有一個(gè) HTML 表單,可將數(shù)據(jù)發(fā)送回與表單輸入相關(guān)的 SharePoint 列表。我目前正在使用所有列表所在的 8 個(gè)不同的子站點(diǎn)。我想根據(jù)其“程序”值將新項(xiàng)目發(fā)送到正確的列表,因?yàn)槊總€(gè)不同的列表都是不同的程序。我知道我必須使用 if/else 語(yǔ)句,但我將如何使用 AJAX 調(diào)用來(lái)實(shí)現(xiàn)它?這是我的 JS“POST”代碼:$("#btn").click(function(e) {            PostItem();        });    });            function PostItem() {            return getFormDigest("https://baseurl.sharepoint.com/sites/Projects/USMC/AMMO/Lists/AMMODeliverables/").then(function(digestData) {                console.log(digestData.d.GetContextWebInformation.FormDigestValue);                var item = {                    "__metadata": { "type": "SP.Data.AMMODeliverablesListItem" },                    "Title": "updated title",                    "Program": $("#dProgram").val(),                    "Deliverable": $("#dDeliverable").val(),                    "To": $("#dTo").val(),                    "Date": $("#dDate").val(),                    "Approved": $("#dApproved").val(),                    "Notes": $("#dNotes").val()                };                                $.ajax({                    async: true, // Async by default is set to “true” load the script asynchronously                      // URL to post data into sharepoint list  or your own url                    url: "https://baseurl.sharepoint.com/sites/Projects/USMC/AMMO/_api/web/lists/getbytitle('AMMO Deliverables')/items",                    method: "POST", //Specifies the operation to create the list item                      data: JSON.stringify(item),                    headers: {                        "content-type": "application/json;odata=verbose",                        "X-RequestDigest": digestData.d.GetContextWebInformation.FormDigestValue,                        "Accept": "application/json;odata=verbose",                        "If-Match": "*"                    }, 
查看完整描述

1 回答

?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

首先,您的getFormDigest功能不太正確:


function getFormDigest(baseurl) {

    

    // you pass in a "baseurl" value above, but you're not really doing anything with it.

    // the URL you use below is hardcoded to always

    // make the request to the /sites/Projects/USMC/AMMO site


    return $.ajax({


        url: "https://baseurl.sharepoint.com/sites/Projects/USMC/AMMO/_api/contextInfo",


        method: 'POST',


        headers: {

            'Accept': 'application/json; odata=verbose'

        }


    });

您需要做的是更改它,以便您可以將站點(diǎn) URL 傳遞給它并從您嘗試將新項(xiàng)目發(fā)布到的實(shí)際站點(diǎn)獲取有效的表單摘要:


function getFormDigest(siteUrl) {

    return $.ajax({

        url: siteUrl + "/_api/contextInfo",

        method: 'POST',

        headers: {

            'Accept': 'application/json; odata=verbose'

        }

    });

}

接下來(lái),您需要更改您的PostItem函數(shù)以對(duì)所選程序的當(dāng)前值做出反應(yīng),并根據(jù)該值選擇一些正確的值。我在評(píng)論中看到您發(fā)布了一個(gè)小片段,您正在創(chuàng)建一個(gè)地圖,該地圖將根據(jù)所選程序的密鑰吐出正確的 URL。如果您只需要一個(gè)值,那是可行的,但是,由于您說(shuō)過(guò)每個(gè)子站點(diǎn)上的列表名稱(chēng)都不同,因此您實(shí)際上需要根據(jù)所選程序動(dòng)態(tài)生成三個(gè)不同的值:

  1. 站點(diǎn)本身的 URL,以便您可以獲得有效的表單摘要,

  2. 列表名稱(chēng),以便您可以為新項(xiàng)目的 JSON__metadata屬性獲取正確的列表項(xiàng)目實(shí)體類(lèi)型值。您有執(zhí)行此操作的功能,但您沒(méi)有使用它。此外,您還需要列表名稱(chēng):

  3. 一個(gè)包含站點(diǎn)和列表名稱(chēng)的URL ,以便您可以發(fā)布新的列表項(xiàng)(因?yàn)檫@樣做的 URL 本質(zhì)上是[URL to site]/_api/web/lists/getbytitle('[list name]')/items

您可以執(zhí)行一系列if..then..else if..then..else if..then..else語(yǔ)句,但是對(duì)于超過(guò)兩個(gè)或三個(gè)可能的值,這會(huì)變得很麻煩。一種更簡(jiǎn)潔的方法是使用語(yǔ)句switch。因此,PostItem如果您使用 aswitch來(lái)評(píng)估所選程序值是什么,然后基于該值動(dòng)態(tài)設(shè)置站點(diǎn) URL 和列表名稱(chēng),那么您的函數(shù)可能如下所示:

function PostItem() {


    // the base URL should be what is the same across all subsites. in comments

    // you said the subsites start to differ after /sites/Projects.

    var baseUrl = "https://your-tenant.sharepoint.com/sites/Projects";


    // get the selected program from your form

    var programName = $("#dProgram").val();


    var siteUrl = null; // set this as empty for now

    var listName = null; // set this as empty for now


    // a "switch" statement is like a fancy "if" statement that is

    // useful if you have more than just two or three options


    switch (programName) {

        case "AMMO":

            // set the site url to be whatever it is after /sites/Projects.

            // in the case of AMMO, you have already posted that the "AMMO"

            // subsite is under a "USMC" subsite that is under "Projects"

            siteUrl = baseUrl + "/USMC/AMMO";

            listName = "AMMODeliverables";

            break;

        case "AHR":

            // set the site url to be whatever it is after /sites/Projects.

            // IF in this case the "AHR" subsite is directly under /Projects

            // and NOT under another subsite (as is the case with /USMC/AMMO),

            // you just add that directly:

            siteUrl = baseUrl + "/AHR";


            // HOWEVER, if it is under another subsite with a different name, similar

            // to how "AMMO" is actually under another subsite called "USMC", then you

            // would include that "Other" subsite here:

            siteUrl = baseurl + "/Other/AHR";


            // set the list name, since you said the list names

            // are different in each of the subsites

            listName = "AHR Deliverables";

            break;

        case "FOO":

            // pretending that "FOO" is _directly_ under /sites/Projects

            siteUrl = baseurl + "/FOO";

            listName = "FOO Thingys";

            break;

        case "BAR":

            // pretending that "BAR" is NOT directly under /sites/Projects,

            // but is in fact under another "Different" subsite

            siteUrl = baseurl + "/Different/BAR";

            listName = "BAR Whatchamacallits";

        default:

            // all switch statements need a default option in case

            // what we are checking does not match any any of the options

            // we are expecting. in this instance, we will _not_ set

            // a site URL or list name so that we do not try to post

            // to s non-existent site or list

            break;

    }


    // if we didn't get one of our expected choices for programName, then siteUrl

    // will not have been populated in the switch, so we can check and make sure we

    // actually have a valid siteUrl before we start sending AJAX requests out

    if (siteUrl) {

        // pass the siteUrl into your improved getFormDigest function so

        // that you get the correct form digest from the site you are

        // actually trying to post a new item to.

        // also, you don't actuall need the "return" here.

        getFormDigest(siteUrl).then(function(digestData) {

            console.log(digestData.d.GetContextWebInformation.FormDigestValue);


            // use your getItemTypeForListName function to get the

            // correct SharePoint List Item Entity Type name based on

            // the list name

            

            var listItemEntityType = getItemTypeForListName(listName);


            // construct the URL to post the new list item to based on the siteUrl

            // and the listName, which vary based on the selected projecName


            var postNewItemUrl = siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items";


            // construct your new item JSON.  you said all the fields

            // in all the lists are the same, so the only thing that really

            // needs to dynamically chage here is the entity type name,

            // which was generated based on the list name

            var item = {

                "__metadata": { "type": listItemEntityType },

                "Title": "updated title",

                "Program": programName,

                "Deliverable": $("#dDeliverable").val(),

                "To": $("#dTo").val(),

                "Date": $("#dDate").val(),

                "Approved": $("#dApproved").val(),

                "Notes": $("#dNotes").val()

            };


            

            $.ajax({ 

                // use your dynamically generated URL here

                url: postNewItemUrl,

                method: "POST", //Specifies the operation to create the list item  

                data: JSON.stringify(item),

                headers: {

                    "content-type": "application/json;odata=verbose",

                    "X-RequestDigest": digestData.d.GetContextWebInformation.FormDigestValue,

                    "Accept": "application/json;odata=verbose",

                    "If-Match": "*"

                },

                success: function(data) {

                    alert('Success'); // Used sweet alert for success message

                    console.log(data + " success in updating item");

                },

                error: function(data) {

                    alert(JSON.stringify(item));

                    console.log(data);


                }


            });

        });

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-01
  • 1 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)