使用 xmlrpc 和 jQuery 创建 WordPress 帖子

发布于 2024-08-21 14:31:43 字数 884 浏览 7 评论 0原文

我有一个动态页面,用户可以在其中填写一些字段。这些用户都将在 WordPress 博客上拥有一个帐户。我想让他们直接将网页生成的内容发布到博客上。我不想将他们的密码存储在服务器中,因此我想使用 JQuery 在客户端执行此操作。

我查看了标准 jQuery.post 方法和 rpc 插件 但我没能让它们工作。例如,我最近的尝试是这样的:

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);
}

和绝望的尝试:

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

但它默默地失败了(甚至没有调用回调)。

你会怎么做?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2024-08-28 14:31:43

虽然正如 Doug 指出的那样,源策略会让您陷入困境,但您可以在一台服务器上使用一个脚本将结果发布到另一台服务器(一种代理)。这有点像黑客,但它会起作用。我个人会使用 PHP 和 cURL 来做到这一点。

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.

几度春秋 2024-08-28 14:31:43

您无法使用 jQuery 或任何其他 JavaScript 技术发出跨域 POST 请求。这是因为出于安全原因需要同源策略。实现此目的的唯一方法是通过与 jQuery 代码相同的域、子域、协议和端口上的服务器代理。

不确定您使用的服务器技术,但您可以查看 简单 PHP 代理本·阿尔曼着。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文