jQuery Ajax通过https

I am having a problem with jQuery's ajax method over https.

The whole site is served over https. Most forms are submitted to a processing page via ajax. The url for the ajax function is basically:

$.ajax({
    "/assets/php/ajax/processor.php",
    ...
});

I also have a .htaccess file which redirects all http requests to https ones like so:

# Redirect any http requests to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L]

The ajax works fine if I remove the redirect in the .htaccess file, but will not seem to work with it there. I assume this is because ajax functions over http (jQuery description of .ajax(): Perform an asynchronous HTTP (Ajax) request) and therefore the .htaccess redirect is catching the ajax request.

I need the redirect because I don't want users of the site to use the http version (there is a sign in header on every page) but i need the ajax to work too and be secured by https.

Thanks in advance for any help

Add the Access-Control-Allow-Origin to your server's headers:

Access-Control-Allow-Origin: https://yoursite.com

Then your original .htaccess file should work as expected.

Change your rule to skip switching to https for the AJAX URI:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/assets/php/ajax/processor\.php [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]