尝试将简单的文本文件添加为通过SharePoint 2013 REST API添加简单文本文件时的“500:内部服务器错误”

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/59898

使用 SharePoint 2013 REST API ,我正在尝试将简单的文本文件附加到列表的项目。

jQuery.ajax({
    url: web.get_url() + "/_api/web/lists/GetByTitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt')",
    type: "POST",
    headers: {
        "body": "Contents of file.",
        "content-length": 17,
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: doSuccesRestAPIAddAttachment,
    error: doErrorRestAPIAddAttachment
  });
.

我收到以下错误:

500:Internal Server Error
<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>-2147024883, Microsoft.SharePoint.SPException</m:code><m:message xml:lang="en-US">The data is invalid. (Exception from HRESULT: 0x8007000D)</m:message></m:error>
.

谁能告诉我我做错了什么?

谢谢。

有帮助吗?

解决方案

有趣的问题!我读到了您链接到的页面。有时候有点令人困惑的说明。在某些实验后,我可以在您完成附件时添加:

$.ajax({
    url: "/_api/web/lists/GetByTitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt')",
    type: "POST",
    data: "Contents of file.",
    headers: {        
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
  });
.

我直接在root web中测试,并没有提供任何回调,只是为了保持简单。我删除了“Content-Length”标题,因为Chrome抱怨“不安全标题”。我把“文件的内容”。进入请求有效载荷(data: "Contents of file.")。以下例子适用于我。说如果它不适合你。

原始答案:

也许它不是完整的答案,但“身体”不应该在里面的标题里面。试着把它放在身体外面。

jQuery.ajax({
    url: web.get_url() + "/_api/web/lists/GetByTitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt')",
    type: "POST",
    body: "Contents of file.",
    headers: {        
        "content-length": 17,
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: doSuccesRestAPIAddAttachment,
    error: doErrorRestAPIAddAttachment
  });
.

许可以下: CC-BY-SA归因
scroll top