Home Page
  • March 29, 2024, 02:44:15 am *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: Cross Domain AJAX Requests  (Read 12229 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 535
    • View Profile
    • Dakusan's Domain
Cross Domain AJAX Requests
« on: December 02, 2009, 02:08:15 am »

Original post for Cross Domain AJAX Requests can be found at https://www.castledragmire.com/Posts/Cross_Domain_AJAX_Requests.
Originally posted on: 12/02/09

Since I just released my AJAX Library, I thought I’d post a useful script that uses it. The function CrossDomainGetURL below uses the AJAX Library to make requests across domains in Firefox. It takes one more parameter (not in order) than the AJAX Library's GetURL function, which is an array of domains to pull cookies from for the AJAX request.



function GetCookiesFromURL(Domains) //Return all the cookies for Domains specified in the Domains array
{
   var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager); //Requires privileges, which is granted in CrossDomainGetURL
   var iter=cookieManager.enumerator, CookieList=[], cookie; //The object used to find all cookies, the final list of cookies, and a temporary object
   while(iter.hasMoreElements()) //Loop through all cookies
      if(((cookie=iter.getNext()) instanceof Components.interfaces.nsICookie) && Domains.indexOf(cookie.host)!=-1) //If a cookie whose host matches one of our domains
         CookieList.push(cookie.name+'='+cookie.value); //Add it to our final list
   return CookieList.join("; "); //Return the cookie list for the specified domains
}

function CrossDomainGetURL(URL, Data, CookieDomains, ExtraOptions) //See AJAX Library GetURL function. CookieDomains is an array specifying what domains cookies are pulled from for the AJAX call.
{
   //Access universal privileges in Firefox (Required to get cookies for other domains, and to use AJAX with other domains). This functionality is lost as soon as this function loses scope.
   try { netscape.security.PrivilegeManager.enablePrivileg e("UniversalXPConnect"); }
   catch(e) { return alert('Cannot access browser privileges'); }

   if(CookieDomains instanceof Array) //If an array of domains is passed to get cookies from...
   {   
      ExtraOptions=((ExtraOptions instanceof Object) ? ExtraOptions : {}); //Make sure extra options is an object
      ExtraOptions.AdditionalHeaders=((ExtraOptions.AdditionalHeaders instanceof Object) ? ExtraOptions.AdditionalHeaders : {}); //Make sure extra options has an additional headers object
      ExtraOptions.AdditionalHeaders.Cookie=GetCookiesFromURL(CookieDomains); //Get cookies for the domains
   }
   
   return GetURL(URL, Data, ExtraOptions); //Do the AJAX Call
}
Logged