Skip to main content
Bumped by Community user
Source Link

How to insert items to list using jquery?

Hi I am doing some work on sharepoint list using jquery. Sharepoint is new environment for me. I have worked in jquery. I am having problem in hitting to List using Ajax. My url will display url is undefined. I have googled it and wrote same piece of code.

This is my jquery code.

<script src=http://ajax.aspnetcdn.com/ajax/jquery-2.1.1.js> </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript">
        function AddNewItem()
        {
            var Title,empID, Description, Quantity, Price,City;
            Title = $("#<%=Title.ClientID%>").val();
            empID = $("#<%=empID.ClientID%>").val();
            Description = $("#<%=description.ClientID%>").val();
            Quantity = $("#<%=Quantity.ClientID%>").val();
            Price = $("#<%=Price.ClientID%>").val();
            City = $("#<%=City.ClientID%>").val();
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('NiranjanTest')/items";
            var data = JSON.stringify(
              {
                  "__metadata":
                  {
                      "type": "SP.Data.NiranjanTestListItem"
                  },
                  "empID": empID,
                  "Title":Title,
                  "Description": Description,
                  "Quantity": Quantity,
                  "Price": Price,
                  "City": City
              });
       
            $.ajax
            ({
               
                type: "POST",
                data: data,
                url: url,
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "Content-Type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success: function (data)
                    
                    {
                    alert("New list item has been created successfully.");
                },
                error: function (data) {
                
                    alert("Failed to create list item.");
                }
            });
        }
    </script>

Can someone tell me why I am not able to make server call? Any help would be appreciated. Thank you...