1

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...

  • Are you facing error in _spPageContextInfo.webAbsoluteUrl? – Viraj Gorajia Nov 21 '16 at 6:13
  • How do you insert the script into SharePoint? Also you can try using F12 developer tool to tackle it. I guess your script is conflict with SharePoint javascript. – Mark L Nov 21 '16 at 6:14
  • This should be breaking on the line where you set url, if you're setting it that way. That's not valid JavaScript. – wjervis Nov 21 '16 at 12:47
2

why are you using

url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('NiranjanTest')/items";

It should be

var url =  _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('NiranjanTest')/items";

Also can you please check below to debug.

  1. are you getting any value in _spPageContextInfo.webAbsoluteUrl ?
  2. Are you running above code in SP context, means is above code running on any SharePoint Page ?
| improve this answer | |

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.