locked
Execute code after "POST" request returns ready RRS feed

  • Question

  • I am trying to update a page using a POST request that is Asynchronous. Synchronous requests = bad so I'd like to correct it. I don't know enough about AJAX to know what variables I should be looking for that indicate the document is ready. Here is my source code; hopefully someone can help correct my error:

       $(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>Produce</listName> \
                                <ViewFields> \
                                   <FieldRef Name='Quantity' /> \
                                   <FieldRef Name='Quantity2' /> \
                               </ViewFields> \
                        </GetListItems> \
                    </soapenv:Body> \
                </soapenv:Envelope>";
            $.ajax({
                //Use an ABSOLUTE reference to your target webservice
                url: "https://mydomain/bitest/_vti_bin/lists.asmx",
                type: "POST",
                dataType: "xml",
                data: soapEnv,
                complete: processResult,
                contentType: "text/xml; charset=\"utf-8\"",
                async: false
            });
    		/*What do I put here to be sure that the AJAX method is done and the data is available?
    		Running the request synchronously is a horrible idea and I don't want to do it... 
    		Most places use the following:
    		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    		}
    		But it doesn't seem to be working for me. Perhaps because my AJAX method doesn't return the
    		same type of information? */
    		alert("Document is ready");
    	})

    Friday, August 10, 2012 6:48 PM

Answers

  • I may have resolved this. I placed the ajax method inside of its own function but left it nested in the $(document).ready function:

    function Poll(){
            $.ajax({
                //Use an ABSOLUTE reference to your target webservice
                url: "https://mydomain/Sandbox/bitest/_vti_bin/lists.asmx",
                type: "POST",
                dataType: "xml",
                data: soapEnv,
                success:processResult,
                complete: Poll,
                timeout: 30000,
                contentType: "text/xml; charset=\"utf-8\"",
                async: true //
    });
    }

    Appears to be working. The method simply reinvokes the same function until it succeeds yet it remains running asynchronously in the background. Thanks to this blog post for giving me some concrete examples of Jquery's polling method.
    • Edited by Shrout1 Monday, August 13, 2012 3:01 PM spelling
    • Marked as answer by Shrout1 Monday, August 13, 2012 3:01 PM
    Monday, August 13, 2012 3:00 PM