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()
    }
  });
.

나는 루트 웹에서 직접 테스트되었으며 콜백을 간단하게 유지하기 위해 콜백을 제공하지 않았습니다.크롬이 "안전하지 않은 헤더"에 대해 불평했기 때문에 "Content-Length"헤더를 제거했습니다.나는 "파일의 내용"을 넣었다.요청 페이로드 (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 ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top