简体   繁体   中英

Cross Server AJAX Call

I found a couple of posts that solved this problem for others, but not for my situation. I would like to get the best solution possible for my cross server ajax call.

Here's the situation. I have two urls, www.mysite.com and members.mysite.com. The first site is wordpress driven and the second is codeigniter driven, I refer to these sites as wordpress side and codeigniter side. The codeiginter side has a calendar page of events that is created as a module so all any page has to do is include the file and all javascript and stuff comes with it. On the wordpress side, we have to include one of these files into a page, but the problem is that the codeigniter side is ajax driven, so when it does the ajax calls, the calls will be cross server. Here are the solutions that I've seen on the web with the cons in my situation:

  1. jsonp - This doesn't support POST data.
  2. simple server side proxy http://devlog.info/2010/03/10/cross-domain-ajax/ - Since the javascript is shared between the two urls, I'll have to create the same file on both the wordpress side and the codeigniter side. This means duplicate code, performance issues, and the fact that if the user is on the codeigniter side, the request has to make an extra page call to get the data.
  3. Symbolic links - since both of these reside on the same server, is it possible to make a symbolic link on the wordpress side? Would my codeigniter settings be loaded? I have a feeling this will not work.
  4. iFrame - This is a module being included, so it lacks all the styles and headers and whatnot
  5. .htaccess file - this is a long shot, but if there was some way to get the .htaccess file to redirect the call to the other server without ajax knowing, it might work?

At this point, I'm leaning to #2 because that's the only working solution. Are there any other solutions that you guys could think of? If there was some way to pass data along with the jsonp, that would be ideal, but all sites say it's impossible to pass post data, and codeigniter doesn't like query strings... actually, I could enable it, but that would probably screw everything up at this point.

UPDATE

So I followed the first posters idea, and it worked. I found a similar solution at jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox What worries me now is that they are say that there are some cross browser issues. Any thoughts?

Would setting the HTTP header "Access-Control-Allow-Origin" in codeigniter site solve your problem? Something like this in your apache site configuration file could do the trick:

    <Directory "/usr/lib/php-lib">
            Order allow,deny
            Allow from all
            Header set Access-Control-Allow-Origin "*"
    </Directory>

    #Instead of "*" you could restrict it to "www.mysite.com"

Or you could set this header inside an .htaccess file inside your page directory

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