javascript – Get Comment Text via REST API

Question

The following REST call is used to get a specific comment:

var comment_id = 239;   
response = wp.apiRequest( {   // ajax post to set up comment redact meta value
                path: 'wp/v2/comments/' + comment_id  ,
                method: 'POST',
                data: thedata
            } );

This returns a JSON object, and the next step is to get the comment text from the JSON object, which contains these values (not complete response, and sanitized):

{
  "id": 239,
  "post": 424,
  "parent": 0,
  "author": 1,
  "author_name": "username",
  "author_url": "",
  "date": "2023-02-20T12:43:02",
  "date_gmt": "2023-02-20T20:43:02",
  "content": {
    "rendered": "<div class=\"comment_text\"><p>here is a comment</p>\n</div>"
  },
  "link": "https://example.com/my-test-post/#comment-239",
  "status": "approved",
  "type": "comment",
}

My problem is I can’t figure out how to get the ‘rendered’ value. I have tried this:

response_array = JSON.parse(response.responseText);

but get an error:

JSON.parse: unexpected character at line 1 column 1 of the JSON data

Still learning JSON, and have done many searches to now avail.

What JS code will get me the comment text out of the response object?

0
Rick Hellewell 1 month 2023-02-22T20:25:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse