简体   繁体   中英

Getting list data from SharePoint 2010 site using Jquery

I am trying to get list data from sharepoint site using JQuery, but have got nothing returned yet, no errors in firebug either. Any clue what is wrong?

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

<script type="text/javascript">
$(document).ready(function() 
{
    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
                 <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <listName>Action Items</listName> \
                    <viewFields> \
                        <ViewFields> \
                           <FieldRef Name='Title' /> \
                       </ViewFields> \
                    </viewFields> \
                </GetListItems> \
            </soapenv:Body> \
        </soapenv:Envelope>";

    $.ajax({
        url: "http://my_site/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
});

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
        console.log("aaaa");
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
    });
}

Right after your line

function processResult(xData, status) { 

add an alert like this:

alert(xData.responseText);

That will show you what is coming back from the call to GetListItems.

also, you should change this line:

 $(xData.responseXML).find("z\\:row").

to this:

$(xData.responseXML).find("[nodeName='z:row']")

which is more reliable across browsers. (See my blog post: http://sympmarc.com/2009/11/08/sharepoints-web-services-jquery-and-the-zrow-namespace-in-safari-and-chrome/ )

As Rob Windsor mentions in his answer, I've got many of the SharePoint Web Services wrapped with jQuery to make them easier to use in my SPServices jQuery library . I'd suggest you try it out, as you won't have to do as much work.

I strongly suggest that you use the client object model instead of the Web services. Much richer functionality and much, much easier to use.

The Client Object Model and jQuery

If you really want to use the Web services, then I suggest you check out the SPServices project .

You should handle the ajax success event, not the complete event. The complete event doesn't have that signature.

http://api.jquery.com/jQuery.ajax/

complete(jqXHR, textStatus)

success(data, textStatus, jqXHR)

Perhaps you have a same-origin policy violation.

Check that the current url where runs the script, begins with http://my_site/

Hope this helps. Cheers

Put the processResult function within $(documnet).ready and check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM