首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Wordpress的JSON创建一个帖子

基于Wordpress的JSON创建一个帖子
EN

Stack Overflow用户
提问于 2015-02-06 15:08:44
回答 3查看 1K关注 0票数 0

我试图在我的网站上创建一个表单,访问者将能够在一个特定的类别中创建一个帖子。问题是,我得到一个404页未找到错误。这是我的代码:

代码语言:javascript
复制
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>"

在控制台上,我得到以下错误:

代码语言:javascript
复制
"NetworkError: 404 Not Found - http://localhost/api/get_nonce/?controller=posts&method=create_post&callback=jQuery111109654319724222027_1423235015042&_=1423235015043"

我现在正在做本地主机。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-06 15:13:54

要使这些URL工作,您应该启用用户友好的permalinks。否则您应该使用?json=get_nonce&controller=posts&method=create_post

票数 0
EN

Stack Overflow用户

发布于 2017-06-12 06:34:55

您的代码运行良好--我将状态更改为草稿数据:{nonce: data.nonce,status:‘草稿’,类别:‘content’,标题:‘xxxx’,内容:‘xxxx’},

票数 0
EN

Stack Overflow用户

发布于 2017-06-12 12:21:26

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

Cookie或以现在为基础的Auth参考下面的URL

http://v2.wp-api.org/guide/authentication/ https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28368732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档