2

Am getting an error 403 when am trying to create a list item. The uri is valid and am able to retrieve list items using 'GET' method. Probably am doing something wrong. Why is the ajax call returning 403 forbiden error? My app is sharepoint hosted. enter image description here

$(document).ready(function(){
    var url = "<mysiteurl>/_api/web/lists/getbytitle('Meeting_rooms')/items";
    var data;

    $('#trigger_btn').click(function(){
        var stime = $('#start_time').val();
        var etime = $('#end_time').val();
        var title = $("#title").val();
        var description = "Lorem ipsum dolor sit amet";
        var room = $("#room").val();

        //data to be 'POST'ed
        data = {
            "__metadata": {'type': 'SP.Data.Meeting_x005f_roomsListItem','Title':"Test" },
            "Title": title,
            "Description": description,
            "EventDate": stime,
            "EndDate": etime,
            "Location": room            
        };

        //REQUESTDIGEST value
        var request_digest;
        $.ajax({
            url: <siteurl>+"/_api/contextinfo",
            type: "POST",
            dataType: 'JSON',
            success: function (data){
                 request_digest = data.FormDigestValue;
            },
            error: function(){
                console.log("request_digest value could not be obtained");
            }
        });

      //Create list item
        $.ajax({
            url: url,
            type:'POST',
            dataType:'JSON',
            headers: {
                        "accept":"application/json; odata=verbose",
                        "X-RequestDigest": request_digest,
                        "content-Type": "application/json; odata=verbose"
                     },
            data: JSON.stringify(data),
            success: function(){
                alert("Your reservation was successful.");
                location.href="index.aspx";
            },
            error: function(error){
                alert("An error occured while trying to make the reservation. Please try again later");
            }
        });
    }); 
});
  • it seems to me that you do not wait for the get to finish before posting, therefore the request digest is not set. – Pantani Aug 3 '16 at 8:20
  • I've tried to fetch the request digest value separately, but am still getting the error when posting. when i console.log the request digest value exists. – Stanley Onchari Aug 3 '16 at 14:45
  • Then I would check everything against what these guys do: plusconsulting.com/blog/2013/05/… – Pantani Aug 4 '16 at 7:41

Your Answer

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

Browse other questions tagged or ask your own question.