发布到查找字段

Posting to a lookup field

我正在尝试 post/Create(不更新)新项目到查找字段。之前关注过example, example,成功了。但出于某种原因,我现在无法进行简单的 Post 调用。我需要额外的一双眼睛来检查我的代码。

查找字段 internalName 是 "questionRelation"。我现在必须在名称后添加 Id 以指定 Lookup 字段代表的列表中的 Id 列。

var data = {
                __metadata: { "type": window._cache.ListItemType },
                Title: user_answer,
                questionRelationId: {
                    'results': parseInt(question_Id)
                }
            };
        var _answers = "answers";
        var _url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + _answers + "')/Items"
        var _type = "POST";
        var _headers = {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        }
        var _data = JSON.stringify(data);
        jQuery.ajax({

            url: _url,
            type: _type,
            headers: _headers,
            data: _data,
            done: function (data, textStatus, jHXR) {},
            error: function (data, textStatus, jHXR) {});

我得到的错误是这样的:

An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected.

我已将查找字段设置为仅采用一个值 (id)。非常感谢输入。

Update

我改了:

   questionRelationId: {
                'results': parseInt(question_Id)
            }

至:

 questionRelationId: parseInt(question_Id)

我成功了。我这样做的方式是当查找列采用数组时。如果它是单个值,则必须删除 results 对象部分。当我查看我自己提供的文档时没有注意。

以下代码在我的本地有效。

<script type="text/javascript">
        var itemProperties = {
            "__metadata": { "type": "SP.Data.ChildListItem" },
            "Title": "RestApiCreated",
            "LookupParentId":1
        };

        function createListItem() {

            $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('child')/items",
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(itemProperties),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "Content-Type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "X-HTTP-Method": "POST"
                },
                success: function (data, textStatus, jqXHR) {
                    alert(data.d.length);
                },

                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR);
                }
            });
        }
    </script>

并尝试使用fiddler来监听请求的raw header来识别header值。