views:

369

answers:

2

Hi,

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:

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 ?

+1  A: 

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.

Doug Neiner
Oh, this is too bad... But then, how does work the firefox extensions which allows to post to your blog (like http://deepestsender.mozdev.org/) for example? I can't touch the WordPress server (it should work with standard wordpress.com blogs). Can you see any other way of solving my problem?
Jim
Sorry for my previous comment. I thought the single origin thing was on the server side but it seems to be in the browser side. I guess my only solution would be to write my own extension. Could be fun :).
Jim
+1  A: 

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.

Xorlev
The thing is it would require this server to see the users passwords (which are part of the xmlrpc request), and I would rather avoid that... Thanks to both of you for pointing me to the Same Origin issue, I guess I'll have to drop that feature and let users copy/paste for now. Later I could probably write my own extension.
Jim