小编tom*_*web的帖子

wp-json API 在使用 React 和 nonce 进行编辑后返回 401

我正在尝试使用 React 在 WordPress 中创建一个管理页面,允许用户管理帖子内容。我已经成功创建了一个删除方法来响应 API,但我在更新工作时遇到困难。

// plugin_file.php
add_action('admin_enqueue_scripts', function() {
    wp_localize_script('tfw-js', 'wpApiSettings', [
        'root' => esc_url_raw( rest_url() ),
         'nonce' => wp_create_nonce( 'wp_rest' )
    ]);
});
Run Code Online (Sandbox Code Playgroud)

上面的代码将该对象转储到我的页面底部附近

<script type='text/javascript'>
/* <![CDATA[ */
var wpApiSettings = {"root":"http:website.com\/wp-
json\/","nonce":"9eb4c99f2c"};
/* ]]> */
</script>
Run Code Online (Sandbox Code Playgroud)

这是按预期工作的删除方法

deletePost(post) {

    var _this = this;
    this.serverRequest =
          axios
            .delete(wpApiSettings.root + "wp/v2/posts/" + post.id, {
                headers: {'X-WP-Nonce': wpApiSettings.nonce},
            })
            .then(function(result) {
                _this.updatePostList();

            })
  }
Run Code Online (Sandbox Code Playgroud)

然而,我的更新方法使用与删除相同的随机数密钥返回 401 未经授权。我不确定使用相同的密钥是否是正确的方法,但 admin-ajax.php 使用相同的随机数密钥,所以我猜测是的。

updatePost(post) {
    var _this = this;
    this.serverRequest =
      axios …
Run Code Online (Sandbox Code Playgroud)

wordpress json reactjs axios

3
推荐指数
1
解决办法
2335
查看次数

为什么没有构造函数参数的类需要括号

我开始学习PHP中的编程/ OOP。据我对PHP最佳实践的了解,如果不带任何参数,则可以实例化没有括号的类。

如

$class = new Class;
Run Code Online (Sandbox Code Playgroud)

相对于:

$class = new Class();
Run Code Online (Sandbox Code Playgroud)

我开始将自己的技能扩展到python中,并于昨天浪费了大约5个小时,试图弄清为什么一个函数即使简单得很简单也不会传递参数。我的代码:

class MainViewWidgets(MainViewContainer):

    def __init__(self):
        # instantiating like this prevents MainViewController.getHeaderItems from returning the arg passed to it, however the code still "works" in some sense
        self.controller = MainViewController 

        #this works
        self.controller = MainViewController()

    def createHeaderOptionCheckbox(self, pane):
        self.header_string = StringVar()
        header_checkbox = ttk.Checkbutton(pane, text='Data Contains Headers', variable=self.header_string, onvalue='headers', offvalue='keys')
        self.header_string.trace('w', self.headerOptionCheckboxChanged)
        return header_checkbox

    def headerOptionCheckboxChanged(self, *args):
        print(self.header_string.get())
        #will print "headers" or "keys" on checkbox toggle
        print(self.controller.getHeaderItems(self.header_string.get()))
        #prints "default" …
Run Code Online (Sandbox Code Playgroud)

python oop tkinter

1
推荐指数
1
解决办法
1416
查看次数

标签 统计

axios ×1

json ×1

oop ×1

python ×1

reactjs ×1

tkinter ×1

wordpress ×1