Welcome! Log In Create A New Profile

Advanced

nginx jquery ajax post error in wordpress wp-e-commerce

Posted by cs 
cs
nginx jquery ajax post error in wordpress wp-e-commerce
June 16, 2010 05:58AM
ahoy,

i'm running wordpress with the wp-e-commerce plugin on nginx and php-fastcgi.
all's fine on apache but on nginx adding a product to a cart was not working at all.

wp-e-commerce uses jquery and ajax to add a product to the cart:

/wp-e-commerce/js/wp-e-commerce.js around line 76:
// Submit the product form using AJAX
jQuery("form.product_form").submit(function() {
// we cannot submit a file through AJAX, so this needs to return true to submit the form normally if a file formfield is present
file_upload_elements = jQuery.makeArray(jQuery('input[type=file]', jQuery(this)));
if(file_upload_elements.length > 0) {
return true;
} else {
form_values = jQuery(this).serialize();
// Sometimes jQuery returns an object instead of null, using length tells us how many elements are in the object, which is more reliable than comparing the object to null
if(jQuery('#fancy_notification').length == 0) {
jQuery('div.wpsc_loading_animation',this).css('visibility', 'visible');
}

// by cs
// for APACHE: jQuery.post( 'index.php?ajax=true', form_values, function(returned_data) {
jQuery.post( '?ajax=true', form_values, function(returned_data) {
eval(returned_data);
jQuery('div.wpsc_loading_animation').css('visibility', 'hidden');
if(jQuery('#fancy_notification') != null) {
jQuery('#loading_animation').css("display", 'none');
//jQuery('#fancy_notificationimage').css("display", 'none');
}
}, 'text');

wpsc_fancy_notification(this);
return false;
}
});

for nginx the line:
jQuery.post( 'index.php?ajax=true', form_values, function(returned_data) {

must be replaced with:
jQuery.post( '?ajax=true', form_values, function(returned_data) {

probably because in my nginx vhost file i have all index.php requests redirected to the website url.
# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}

ie. curl http://example.com works fine and curl http://example.com/index.php fails

it took me one day to figure out this issue i hope it will help someone someday .....
Re: nginx jquery ajax post error in wordpress wp-e-commerce
June 17, 2010 03:50AM
Hi cs,

I am having the same issue with nginx and wp-e-commerce.

Did you only change that one line in /wp-e-commerce/js/wp-e-commerce.js to get it working? I'm still having the same problem with the suggested change.
cs
Re: nginx jquery ajax post error in wordpress wp-e-commerce
August 04, 2010 08:09AM
Yes, only the wp-e-commerce.js

However other plugins are needed to be modified in the same way to work .. so now I'm looking for a general solution when .js files must not be modified.
cs
Re: nginx jquery ajax post error in wordpress wp-e-commerce
August 05, 2010 06:35AM
I couldn't find a solution how to enable AJAX on NGINX with Wordpress.
On Apache all AJAX plugins are working fine, on Nginx none of them.

I suspect the problem lies in the nginx config file with rewrite rules. There you are with nginx.conf:
user www-data;
worker_processes 4;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
# multi_accept on;
}

http {
include /etc/nginx/mime.types;

access_log /var/log/nginx/access.log;

sendfile on;
tcp_nopush on;

keepalive_timeout 30;
tcp_nodelay off;

gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml$
#gzip_disable "MSIE [1-6]\.(?!.*SV1)";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}



... and my site configuration:


server {
listen 80;
server_name xxx;

access_log /xxx/access.log;
error_log /xxx/error.log;

location / {
root /xxx/public;
index index.html index.htm index.php;

# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}

# this sends all non-existing file or directory requests to index.php
#if (!-e $request_filename) {
# rewrite ^(.+)$ /index.php?q=$1 last;
#}
if (!-e $request_filename) {
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
}


#include /etc/nginx/wordpress-params.regular;
include /etc/nginx/wordpress-params.super-cache;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /xxx/public$fastcgi_script_name;
include fastcgi_params;
}
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 259
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready