admin管理员组

文章数量:1126114

I'm using backbone in my website to access the Wordpress API. This works very nicely. However, it is my understanding that backbone always sends a nonce with each request.

This appears to be interfering with caching REST responses, as each request, with a nonce, is treated differently.

Is it possible to remove this nonce from the backbone request?

I'm using backbone in my website to access the Wordpress API. This works very nicely. However, it is my understanding that backbone always sends a nonce with each request.

This appears to be interfering with caching REST responses, as each request, with a nonce, is treated differently.

Is it possible to remove this nonce from the backbone request?

Share Improve this question asked Jan 21, 2024 at 12:46 MastaBabaMastaBaba 3113 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Yes, the nonce is by default always being sent via the X-WP-Nonce header – see the source here and here on GitHub.

wp.api.WPApiBaseModel.prototype.sync and wp.api.WPApiBaseCollection.prototype.sync can technically be extended or modified, but I would instead disable the nonce header like so, i.e. using <Collection or Model object>.endpointModel:

  • Collection example:

    const Posts = new wp.api.collections.Posts();
    
    // Remove the nonce to disable the X-WP-Nonce header.
    Posts.endpointModel.set( 'nonce', '' );
    //Posts.endpointModel.unset( 'nonce' ); // This also works.
    
    Posts.fetch( {
        data: { per_page: 2 },
    } ).done(
        data => console.log( data )
    );
    
  • Model example:

    const Post = new wp.api.models.Post( { id: 1 } );
    
    // Remove the nonce to disable the X-WP-Nonce header.
    Post.endpointModel.set( 'nonce', '' );
    //Post.endpointModel.unset( 'nonce' ); // This also works.
    
    Post.fetch().done(
        data => console.log( data )
    );
    

本文标签: rest apiUsing backbonecan I prevent the nonce from being set