Create a WordPress post with xmlrpc and jQuery

I have a dynamic page where the user can fill some fields. Those users will all have an account on a WordPress blog. I would like to allow them to directly post the content generated by the webpage to the blog. I don’t want to store their password in the server so I want to do this client-side with JQuery.

I have looked both at the standard jQuery.post method and the rpc plugin but I didn’t manage to make them work. For example, my latest attempts were something like this:

Read More
wprpc = $.rpc('http://blog.wordpress.com/xmlrpc.php', 'xml', callback);
function callback(server) {
    answer = server.newPost(0,'user','pass','<struct><title>TestRPC</title></struct>');
    alert(answer);
}

and a desperate one:

$.post('http://blogurl.com/xmlrpc.php', { blogid: 0, username: "user", password: "pass", struct: "<struct><title>Test</title></struct>" }, function(data) {alert(data);}, 'xml');

but it silently failed (the callback is not even called).

How would you do this ?

Related posts

Leave a Reply

2 comments

  1. While the origin policy will trip you up as Doug pointed out, you could have a script on one server post the results to the other, a sort of proxy. It’s a bit of a hack, but it’d work. I’d personally use PHP and cURL to do it.

  2. You cannot make a cross domain POST request using jQuery or any other JavaScript technology. This is because of the same origin policy required for security reasons. The only way you will be able to accomplish this is via a server proxy on the same domain, subdomain, protocol and port as the jQuery code.

    Not sure what server technology you are using, but you could look into Simple PHP Proxy by Ben Alman.