admin管理员组

文章数量:1278976

N.B. Resolved - I hadn't noticed it was a POST rather than a GET.

I'm trying to use an AJAX call from a front end page to pull a list "Staff" (custom post type). I can get the data in the browser via wp-json/wp/v2/staff but I get a 401 when I try to pull the data via AJAX. I'm assuming I need to authenticate somehow but can't work out how to to do it.

Thanks in advance.

N.B. Resolved - I hadn't noticed it was a POST rather than a GET.

I'm trying to use an AJAX call from a front end page to pull a list "Staff" (custom post type). I can get the data in the browser via wp-json/wp/v2/staff but I get a 401 when I try to pull the data via AJAX. I'm assuming I need to authenticate somehow but can't work out how to to do it.

Thanks in advance.

Share Improve this question edited Nov 1, 2021 at 9:28 Codescribler asked Oct 27, 2021 at 15:51 CodescriblerCodescribler 1216 bronze badges 8
  • Have you read the docs? developer.wordpress/rest-api/using-the-rest-api/… – Jacob Peattie Commented Oct 28, 2021 at 7:16
  • All those docs talk about making changes on the PHP side. Surely that's not required!? – Codescribler Commented Oct 28, 2021 at 7:19
  • Depending on what you’re trying to do, yes it is. Normally custom post type endpoints don’t require authentication, but if that’s been added then you need to follow those steps to ensure your request is authenticated. – Jacob Peattie Commented Oct 28, 2021 at 7:45
  • I can explore all the data I need by just going to the wp-jason url I posted above. I could probably scrape it which seems utterly ridiculous. But I can’t get it via Ajax. Something is wrong with that. – Codescribler Commented Oct 28, 2021 at 7:47
  • 1 There’s something wrong with your AJAX request then. But there’s no code in your question so it’s not possible to say more. It sounds like you might be sending a POST request instead of GET. – Jacob Peattie Commented Oct 28, 2021 at 7:50
 |  Show 3 more comments

1 Answer 1

Reset to default 1

I had accidentally created a POST rather than a GET. Obviously, you need credentials if you post.

本文标签: I39m getting a 401 while calling the wpjson endpoint via ajax