簡體   English   中英

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"

我正在使用localhost。

為了使這些URL起作用,您應該啟用用戶友好的永久鏈接。 否則,您應該使用?json = get_nonce&controller = posts&method = create_post

您的代碼運行正常,我將狀態更改為草稿數據:{nonce:data.nonce,狀態:'draft',類別:'mycategory',標題:'xxxx',內容:'xxxx'},

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

基於Cookie或Nonce的身份驗證指的是以下網址,
http://v2.wp-api.org/guide/authentication/ https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM