Wordpress上的JSON API-创建一个帖子


JSON API on Wordpress - creating a post

我试图在我的网站上创建一个表单,访问者可以在其中的特定类别中发帖。问题是我得到了一个404页面未找到的错误。这是我的代码:

echo "<textarea cols='50' rows='10' style='font-size: 24px;'></textarea><br><br>";
echo "<button id='sendmessage' style='padding:10px'>Submit</button>";
echo "<script>
    jQuery('#sendmessage').click(function(e){
        e.preventDefault();
        jQuery.ajax({
        // get the nonce
        dataType: 'jsonp',
        url: '/api/get_nonce/?controller=posts&method=create_post',
        type: 'GET',
        success: function  (data) {
            // create the post
            jQuery.ajax({
                url: '/api/create_post/',
                type: 'POST',
                dataType: 'jsonp',
                data: {nonce: data.nonce, status:'publish', categories:'mycategory', title:'xxxx', content:'xxxx'},
                success: function  (data) {
                },
                error: function  (data) {
                    console.log('error');
                }
            });
        },
        error: function  (data) {
            console.log('error');
        }
        });
    });
    </script>"

在控制台上,我得到这个错误:

"NetworkError: 404 Not Found - http://localhost/api/get_nonce/?controller=posts&method=create_post&callback=jQuery111109654319724222027_1423235015042&_=1423235015043"

我现在正在本地主机上工作。

要使这些URL正常工作,您应该启用用户友好的永久链接。你还应该用什么?json=get_nonce&controller=posts&method=create_post

您的代码运行良好,我将状态更改为draft数据:{nonce:data.nonce,状态:'raft',类别:'mycategory',标题:'xxxx',内容:'xxxy'},

您可以使用下面的WP REST API create post方法,创建帖子的参考URL:https://developer.wordpress.org/rest-api/reference/posts/#create-a后

Cookie或基于非CE的Auth引用以下URL,
http://v2.wp-api.org/guide/authentication/https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

相关文章: