Advertisement
Guest User

Ajax call

a guest
Jan 17th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var savedItemID;
  2.    
  3.     function CreateNewItem(title, listName) {
  4.         var batch =
  5.             "<Batch OnError=\"Continue\"><Method ID=\"1\" Cmd=\"New\"><Field Name=\"Title\">" + title + "</Field></Method></Batch>";
  6.    
  7.         var soapEnv =
  8.             "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
  9.            <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
  10.                xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
  11.                xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
  12.              <soap:Body> \
  13.                <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \
  14.                  <listName>" + listName + "</listName> \
  15.                  <updates> \
  16.                    " + batch + "</updates> \
  17.                </UpdateListItems> \
  18.              </soap:Body> \
  19.            </soap:Envelope>";
  20.    
  21.         var beforesendArg = function (xhr) {
  22.             xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
  23.    
  24.         };
  25.    
  26.         var onsuccessArg = function (data) {
  27.             $(data).find('z\\:row').each(function () {
  28.                 savedItemID = $(this).attr('ows_ID');
  29.             });
  30.         };
  31.    
  32.         var onfailArg = null;
  33.    
  34.         AjaxCall("POST", siteCollection + "/_vti_bin/lists.asmx", false, soapEnv, "text/xml; charset=utf-8", "xml", beforesendArg, onsuccessArg, onfailArg);
  35.        
  36.    
  37.     }
  38.    
  39.    
  40.     function AjaxCall(typeArg, urlArg, cacheArg, dataArg, contentTypeArg, datatypeArg, beforesendArg, onsuccessArg, onfailArg) {
  41.    
  42.         //set a default contenttype if not specified
  43.         if (contentTypeArg === null) {
  44.             contentTypeArg = 'application/x-www-form-urlencoded';
  45.         }
  46.    
  47.         if (onfailArg === null) {
  48.             onfailArg = function (XMLHttpRequest, textStatus, errorThrown) {
  49.                 alert('AjaxCall has failed with the following error message: ' + errorThrown);
  50.             }
  51.         };
  52.    
  53.         $.ajax({
  54.             type: typeArg,
  55.             cache: cacheArg,
  56.             url: urlArg,
  57.             data: (dataArg),
  58.             contentType: contentTypeArg,
  59.             dataType: datatypeArg,
  60.             beforeSend: beforesendArg,
  61.             success: onsuccessArg,
  62.             error: onfailArg
  63.         });
  64.    
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement