Web development Birmingham

Jquery Ajax Cross-site error

By Andrew Ward By Andrew Ward Oct 15, 2012 hints, tips, advice
general page

This article is all about how to get jQuery AJAX cross-site calls working correctly. It assumes that you operate the server that the cross-site AJAX code is communicating to.

When configuring jQuert AJAX calls to work across different servers we kept getting the error "a is null" in the jQuery AJAX response. In particluar I was trying to get the AJAX call to work using XML as the communication method. After looking around online for a bit we found lots of cross site solutions ranging from using jquery json variations instead through to setting up some proxy code in PHP to grab the response for you.

These are overkill and there is a really simple solution. Just add this following line of code to the PHP file that the jQuery AJAX is trying to communicate to:
header('Access-Control-Allow-Origin: *'); 

This simple line of code tells the server that it will allow cross-site communications to it... Which is what the AJAX will be trying to do. I'm not sure about the answer for ASP.NET servers but there is bound to be a similar solution.

Did you find this article useful? Sharing is a great way to show support, and ensures that we can continue to create more quality articles:

Get a free quote