We help IT Professionals succeed at work.

How to find where my JavaScript code is failing

Hello,

I am trying to use the attached code to add a new item to an existing SharePoint list. The code produces a text box and a button on the page. The idea is to type in a title, click the button, and a new record is added to the Campaign list. When I click the button, however, I receveid an alert that simply states, "error". Not very helpful. I have been trying to use the F12 debugger, but so far have not been able to see where the error actually occurrs. It goes off into the jquery.js file and bounces around back and for for a long time before finally stoping and spitting out the usesless "error". But once the error comes, I still don't see what line of the code is actually causing the error.  The error is being provided by the processResult function.

I will be VERY greatful if you would help me figure this out. I hope I have explained this good enough. Let me know if not.

Thank you!
Riverwalk
<%@ Page Language="C#" masterpagefile="~masterurl/custom.master" title="Untitled 1" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderAdditionalPageHead">

		
</asp:Content>
<asp:Content id="Content2" runat="server" contentplaceholderid="PlaceHolderMain">

<script type="text/javascript" src="http://xxxxxx.zzz.comxxxxx/xxxx/xxxxxx/javascript/jquery-1.3.2.min.js"></script>


<script type="text/javascript">


$(function DisplayItem()
{

var test;
test=GetListItem ($().SPServices.SPGetQueryString()["ID"],"Title","Campaign");
alert (test);

});


////////////////////////////////////////////////////////////////////////////////////////

function CreateNewItem(title) {
    var batch =
        "<Batch OnError=\"Continue\"> \
            <Method ID=\"1\" Cmd=\"New\"> \
                <Field Name=\"Name\">" + title + "</Field> \
            </Method> \
        </Batch>";

    var soapEnv =
        "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
        <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
            xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
            xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
          <soap:Body> \
            <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \
              <listName>Campaign</listName> \
              <updates> \
                " + batch + "</updates> \
            </UpdateListItems> \
          </soap:Body> \
        </soap:Envelope>";

    $.ajax({
        url: "http://xxxxx.yyyy.com/_vti_bin/lists.asmx",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("SOAPAction",
            "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
        },
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=utf-8"
    });
}

function processResult(xData, status) {
    alert(status);
}

$(document).ready(function() {
    $("#newTaskButton").click(function() {
        CreateNewItem($("#newTaskTitle").val());
    });
}); 

</script>



	<p>
        Task Title: 
        <input id="newTaskTitle" type="text" />
        <input id="newTaskButton" type="button" value="Create Task" />
    </p>

</asp:Content>

Open in new window

Comment
Watch Question

Michel PlungjanIT Expert
Top Expert 2009

Commented:
you can try adding some alerts or try debugging in firefox
RiverWalkOwner | Database & Web Developer

Author

Commented:
Thanks for the suggestion. I am in the terrible situation of not knowing JavaScript very well at all, yet have to get this working. Can you give me an exact example of what alert to add? I have tried placing a breakpoint in the code, but when the code runs the debugger does not indicate (from what I can tell) exactly what line of code is causing the error.

Also, do you see anything wrong with the code? Maybe you or someone can spot a problem?
Maybe someone with SharePoint access could try to reproduce the error. All that is needed is to create a list named Campaign and with the code I pasted in you will have the exact same scenario, only needing to change the url references to your server name. The code I pasted in, is all of the content of the .aspx file I am using.

Thank you!
Riverwalk

Commented:
try to examine xData variable.
Change alert(status) to alert (xData) in row 63 of your script

Michel PlungjanIT Expert
Top Expert 2009

Commented:
and update to latest jQuery which is 1.7.1
RiverWalkOwner | Database & Web Developer

Author

Commented:
ruslanin -- Thank you for responding. I did as you suggested and I get an error, "[object Object]". Do you know what that means?
mplungian -- I am going to try your suggestion next. Thank you as well.

Riverwalk
RiverWalkOwner | Database & Web Developer

Author

Commented:
mplungian -- I have update the  JQuery, but still have the same issue. Also, the example that I took this from uses a much earlier version of JQuery. I got the example from here: http://weblogs.asp.net/jan/archive/2009/04.aspx
Owner | Database & Web Developer
Commented:
A coworker helped me with this. I was not referencing the _vti_bin/lists.asmx file correctly.

Thanks for your help.

Riverwalk
RiverWalkOwner | Database & Web Developer

Author

Commented:
A coworker helped me figure this out.
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Great