最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - How to make a fetch() POST request to wordpress rest api?

programmeradmin0浏览0评论

I am trying to make a POST request to rest api. But I am getting 401 Unauthorised error. Also can someone help with handling nonce in it?

#OurPostData : Contains the title.

fetch('/wp-json/wp/v2/code',{
      method: 'POST',
      credentials: 'same-origin',
      headers: new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
      body:JSON.stringify(OurPostData),
    }).then(response => {
      console.log(response);
      return response.json();
    });

this method below works

var createPost = new XMLHttpRequest();
createPost.open("POST", "/wp-json/wp/v2/code");
createPost.setRequestHeader("X-WP-Nonce", Ajax.nonce);
createPost.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
createPost.send(JSON.stringify(OurPostData));
createPost.onreadystatechange = function() {
  if (createPost.readyState == 4) {
    if (createPost.status == 201) {
      alert("Success");
    } else {
      alert("Error Try Again");
    }
  }
}

Thanks

I am trying to make a POST request to rest api. But I am getting 401 Unauthorised error. Also can someone help with handling nonce in it?

#OurPostData : Contains the title.

fetch('https://mywebsite.online/wp-json/wp/v2/code',{
      method: 'POST',
      credentials: 'same-origin',
      headers: new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
      body:JSON.stringify(OurPostData),
    }).then(response => {
      console.log(response);
      return response.json();
    });

this method below works

var createPost = new XMLHttpRequest();
createPost.open("POST", "https://mywebsite.online/wp-json/wp/v2/code");
createPost.setRequestHeader("X-WP-Nonce", Ajax.nonce);
createPost.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
createPost.send(JSON.stringify(OurPostData));
createPost.onreadystatechange = function() {
  if (createPost.readyState == 4) {
    if (createPost.status == 201) {
      alert("Success");
    } else {
      alert("Error Try Again");
    }
  }
}

Thanks

Share Improve this question edited Jan 9, 2021 at 14:02 asked Jan 9, 2021 at 13:22 user145078user145078 7
  • Lots of other people with this issue before - for example: wordpress.stackexchange/a/315731/7968 – Q Studio Commented Jan 9, 2021 at 13:29
  • 1 Also, from the official documentation on authentication: developer.wordpress/rest-api/using-the-rest-api/… – Q Studio Commented Jan 9, 2021 at 13:30
  • @QStudio is there a way to do it without that way? If I am making something for the public I can't ask for them to edit htaaccess file – user145078 Commented Jan 9, 2021 at 13:31
  • First, try and see if this is the issue, then consider the technical implications. – Q Studio Commented Jan 9, 2021 at 13:33
  • 1 @QStudio It was a nonce issue – user145078 Commented Jan 9, 2021 at 14:07
 |  Show 2 more comments

1 Answer 1

Reset to default 2

'X-WP-Nonce' : Ajax.nonce Was missing that's why it was giving the error

  fetch('https://mywebsite.online/wp-json/wp/v2/code', {
      method: 'POST',
      credentials: 'same-origin',
      headers: new Headers({
        'Content-Type': 'application/json;charset=UTF-8',
        'X-WP-Nonce' : Ajax.nonce
      }),
      body: JSON.stringify(OurPostData),
    }).then(response => {
      console.log(response);
      return response.json();
    });
发布评论

评论列表(0)

  1. 暂无评论