Look at the URL in the error:
http://solucase.com/wp-json/wp/v2/posts?categories=%27cat_id%27
%27
is the URL encoded character for '
, so you can see what’s happening is that in the URL you are requesting with AJAX cat_id
is not being replaced with the variable value.
This is because the way you have used quotes when defining the URL is incorrect:
"http://solucase.com/wp-json/wp/v2/posts?categories="cat_id""
Firstly, you have opened the string with double quotes, "
. but are attempting to close them with single quotes, '
. You can’t close double quotes with single quotes, so the quotes are just becoming part of the string, same with the cat_id
variable name.
Secondly, after attempting to close the quotes you are not properly concatenating the variable cat_id
onto the string. To concatenate strings in JS you need to use the +
character.
So the url
argument in your AJAX call should be:
url: "http://solucase.com/wp-json/wp/v2/posts?categories=" + cat_id
Or
url: 'http://solucase.com/wp-json/wp/v2/posts?categories=" + cat_id
Either will work.
Also note that you are not adding anything after the category ID, so you don”t need to open and close the quotes again.