简体   繁体   English

发出 POST 请求时出现 400 错误(将项目添加到 SharePoint 列表)

[英]400 error when making POST request (adding item to SharePoint list)

I have a form that contains check boxes and input fields, and hitting the Submit button should ideally add the content values to a SP list.我有一个包含复选框和输入字段的表单,点击提交按钮应该理想地将内容值添加到 SP 列表中。

When I run the debugger I can see that each input (checked items, text inputs, etc) is getting passed into addItemToSPList , but when I open the Network tab I get the following error: "value: "A node of type 'StartArray' was read from the JSON reader when trying to read a value of a property;当我运行调试器时,我可以看到每个输入(检查项目、文本输入等)都被传递到addItemToSPList中,但是当我打开“网络”选项卡时,我收到以下错误: “value:”A node of type 'StartArray'在尝试读取属性值时从 JSON 读取器读取; however, a 'PrimitiveValue' or 'StartObject' node was expected."但是,需要一个 'PrimitiveValue' 或 'StartObject' 节点。”

I'm suspecting that something's wrong within my ajax block (perhaps the url), but I'm not 100% sure.我怀疑我的ajax块(可能是 url)中有问题,但我不是 100% 确定。

Any thoughts on what's going on?有什么想法吗?


JS code: JS代码:

  handleClick() {
    let specialtiesArr = [],
        regionsArr = [],
        commentsArr = [],
        nameArr = [];
    
    $(".check-spec:checked").each(function() {
        specialtiesArr.push($(this).val());
    })

    $(".check-region:checked").each(function() {
        regionsArr.push($(this).val());
    })

    commentsArr.push($('.request-text-area').val());
    nameArr.push($('.submitter-name').val());

    addItemToSPList(specialtiesArr, regionsArr, commentsArr, nameArr)
  }

} // export default class closing bracket

  function addItemToSPList(getSpecialties, getRegions, getComments, getSubmitterName) {
        let specialistRequestsColumns = {
            "__metadata":{"type": "SP.Data.Specialist_x0020_RequestsListItem"},
            "Title": "No Title", // marked it as "not required" in the SP list backend
            "Specialties": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: getSpecialties }, // multi-select checkboxes
            "Regions": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: getRegions }, // multi-select checkboxes
            "Comments": getComments, // text box
            "Submitter_x0020_Name": getSubmitterName // input field
        }
    
        let listName = "Specialist%20Requests";
        $.ajax({
            url: `${_globalUrl}/redacted/_api/web/lists/getByTitle('${listName}')/items`, // ?$select=ListItemEntityTypeFullName
            method: "POST", // type
            async: false,
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(specialistRequestsColumns),
            xhrFields: {
                withCredentials: true
            },
            headers: {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: data => {
                console.log("upload successful")
                // console.log(data)
            },error: (err) => {
                console.log("Error: " + err);
            }
        })
    }

I got an answer from the nice folks on the SharePoint Stack Exchange.我从 SharePoint Stack Exchange 上的好人那里得到了答案。

Basically, the Comments and Submitter_x0020_Name columns were expecting strings , so when I was passing array values it threw the error.基本上, CommentsSubmitter_x0020_Name列需要strings ,所以当我传递数组值时它会抛出错误。

Here's the updated code:这是更新的代码:

    handleClick() {
        let comments, submitterName;
        let specialtiesArr = [],
            regionsArr = [];
        
        $(".check-spec:checked").each(function() {
            specialtiesArr.push($(this).val());
        })

        $(".check-region:checked").each(function() {
            regionsArr.push($(this).val());
        })

        comments = $('.request-text-area').val();
        submitterName = $('.submitter-name').val();

        addItemToSPList(specialtiesArr, regionsArr, comments, submitterName)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM