【问题标题】:JSON API on Wordpress - creating a postWordpress 上的 JSON API - 创建帖子
【发布时间】:2015-02-06 15:08:44
【问题描述】:

我尝试在我的网站上创建一个表单,访问者可以在其中发布特定类别的帖子。问题是我收到 404 page not found 错误。这是我的代码:

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"

我现在在 localhost 上工作。

【问题讨论】:

    标签: php jquery ajax json wordpress


    【解决方案1】:

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

    【讨论】:

    • 现在我有 200 个可以获取和发布但帖子不存在。
    【解决方案2】:

    您的代码运行良好,我将状态更改为草稿 数据:{nonce: data.nonce, status:'draft', categories:'mycategory', title:'xxxx', content:'xxxx'},

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多