JSON API 在 Wordpress 上 - 创建一个 post

JSON API on Wordpress - creating a post

我尝试在我的网站上创建一个表单,访问者可以在其中创建特定类别的 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

你的代码工作正常我将状态更改为草稿 数据:{随机数:data.nonce,状态:'draft',类别:'mycategory',标题:'xxxx',内容:'xxxx'},

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

基于 Cookie 或 Nonce 的身份验证参考下面 URLs,
http://v2.wp-api.org/guide/authentication/ https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/