Working with jQuery in wordpress can be a chore sometimes but once you know the techniques required it becomes second nature. Now if you are using wordpress on nginx you might find your ajax calls don’t work and you have no reason why. Here is a basic example:

Simple Javascript function

Here is the test javascript, lets say it exists in test.js to do an AJAX call:

1
$.post('test.php', {name: "Steve"}, function(returnedData){ alert(returnedData);});

As you can see it’s a very simple AJAX call which passes a variable called name to a file called test.php and the callback function just alerts whatever is returned from the server.

Simple PHP script

Here is the PHP script that will be called by the javascript to return a value.

1
2
3
<?php 
echo 'Hello world';
?>

This script is clearly not rocket science. All it is doing is printing out “Hello world” and exiting. Now what is expected on the client side?
All the should happen is that an alert box should pop-up displaying the words “Hello world” in it, but is that what really happens? If you are using nginx as your HTTP server then the answer is no. For some reason the header on the HTTP response is not set so all you need to remedy this add you jsut need to it in yourself.

1
2
3
4
<?php 
header("HTTP/1.1 200 OK");
echo 'Hello world!';
?>

And that should do it! hopefully this will save you from hours of trying find and fix this problem.

Filed under: Help jQuery Wordpress