Dogmatics
Dogmatics

Reputation: 67

Access Wordpress ajax on external host

I'm building this HTML/JS app to turn into mobile with Phonegap that is based on my wordpress site (using its database, users, etc.). So right now I have an index.html with a functions.js that tries to access a function created on my wordpress like

add_action('wp_ajax_nopriv_checking_email', 'checking_email');

what I want is to be able to access this with my JS code. I've tried something like this:

jQuery.ajax({
    type: 'POST',
    url: 'http://myhost.com/wp-admin/admin-ajax.php',
    data: { 'action': 'checking_email', 'email': '[email protected]' },
    success: function(request_data) {
        console.log(request_data);
    }
});

and I get

XMLHttpRequest cannot load http://myhost.com/wp-admin/admin-ajax.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

What can I do to access admin-ajax.php from an external host?

Upvotes: 2

Views: 1931

Answers (1)

Dogmatics
Dogmatics

Reputation: 67

So, after testing a bit more I found that it was actually a pretty silly fix. By changing the header on the server's script it allowed access to it from a different host.

header('Access-Control-Allow-Origin: *'); // for any host
header('Access-Control-Allow-Origin: myhost.com'); // specific host

Upvotes: 1

Related Questions