I am using this link for reference to fetch data from document library using Rest api.

The issue I am facing is I am getting junk data in result. Below is my code sample:

<button type='button' onclick="retriveItems()">Copy document</button>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type='text/javascript'>

    function retriveItems() {
    alert("Hello World");

    var requesturi= "";
    if((_spPageContextInfo.webAbsoluteUrl) && (_spPageContextInfo.webAbsoluteUrl.indexOf("sites")>0))
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('"+_spPageContextInfo.webServerRelativeUrl+"/Shared%20Documents/teghjhs.docx')/$value";
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('/Shared%20Documents/teghjhs.docx')/$value";
            url: requesturi,
            type: "GET",   
                "accept": "application/json;odata=verbose",  
                "content-type": "application/json;odata=verbose",
            cache: false,  
            success: function(data)   
            error: function(data)  
                console.log('error occured');  

Below is output screenshot when I hover on data or add it in console:

enter image description here

enter image description here

Can someone suggest me how can I get data in correct json output?


There is nothing wrong with your code.

It's the format of the document you are trying to read (i.e. docx).

SharePoint doesn't provide API to read the word file content. If you will try to read any other file with .txt format from your document library, it will read in proper format.

Refer this thread for more information :


  • your provided link's accepted answer has two links. One redirect me to same stack overflow link I go through. The other uses word interop. I can't use word interop with SharePoint Online. Nov 11 '17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.