2015-09-07 20 views
7

Tôi đang cố gắng để tạo ra một mục danh sách đơn giản với api còn lại trên Sharepoint 2013. Mã của tôi:Sharepoint 2013 qua REST API: Lỗi 403 Forbidden khi cố gắng tạo mục

$.ajax({ 
    url: siteUrl + "/_api/web/lists/getByTitle('internal_Listname')/items", 
    type: "POST", 
    contentType: "application/json;odata=verbose", 
    data: JSON.stringify({ 
     '__metadata': { 
      'type': 'SP.Data.internal_ListnameListItem', 
     }, 
     'K1F1': k1f1Result, 
    }), 
    headers: { 
     "accept": "application/json;odata=verbose", 
     "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
    }, 
    success: function (data) { 
     console.log("done"); 
    }, 
    error: function (err) { 
     console.log(JSON.stringify(err)); 
    } 
}); 

Khi cố gắng gửi dữ liệu tôi nhận được lỗi "Cấm" 403.

"error":{ 
    "code":"-2130575251, Microsoft.SharePoint.SPException", 
    "message":{ 
     "lang":"en-US", 
     "value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again." 
    } 
} 
  • tôi có đặc quyền quản trị đầy đủ trên trang web này và danh sách.

Trả lời

1

Tìm thấy giải pháp cách đây vài ngày: Tôi quên để thêm yêu cầu tiêu hóa dưới hình thức cho cơ thể. Nó phải có cấu trúc sau;

<form runat="server"> 
    <SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest> 
</form> 
10

Rất có thể lỗi này xảy ra vì quá trình phân loại biểu mẫu đã là hết hạn trên trang.

Trong trường hợp đó, bạn có thể nhận được giá trị thông báo biểu mẫu mới bằng cách thực hiện yêu cầu POST tới /_api/contextinfo điểm cuối.

Ví dụ

function getFormDigest(webUrl) { 
    return $.ajax({ 
     url: webUrl + "/_api/contextinfo", 
     method: "POST", 
     headers: { "Accept": "application/json; odata=verbose" } 
    }); 
} 


function createListItem(webUrl, listName, itemProperties) { 
    return getFormDigest(webUrl).then(function (data) { 

     return $.ajax({ 
      url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", 
      type: "POST", 
      processData: false, 
      contentType: "application/json;odata=verbose", 
      data: JSON.stringify(itemProperties), 
      headers: { 
       "Accept": "application/json;odata=verbose", 
       "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue 
      } 
     }); 
    }); 
} 

Cách sử dụng

//Create a Task item 
var taskProperties = { 
    '__metadata': { 'type': 'SP.Data.WorkflowTasksItem' }, 
    'Title': 'Order approval' 
}; 

createListItem(_spPageContextInfo.webAbsoluteUrl, 'Workflow Tasks', taskProperties) 
.done(function (data) { 
    console.log('Task has been created successfully'); 
}) 
.fail(function (error) { 
    console.log(JSON.stringify(error)); 
}); 
0

Giải pháp của tôi để cùng một vấn đề:

<form id="form1" runat="server"> <!-- this make SP 2013 take it legit --> 
<div class="style1"> <!-- dont know what, but SP need it --> 
---your page usually a divs--- 
</div> 
</form> 
Các vấn đề liên quan