Home » Jquery » javascript – how can I send a data by ajax and the url has a variable value in the middle of the link

javascript – how can I send a data by ajax and the url has a variable value in the middle of the link

Posted by: admin February 22, 2020 Leave a comment

Questions:

I am using this API:’/api/categories/${catID}/items/addItem/’

from my spring boot java to enter some data to item table which has relation with category table.

Now in javascript, I used ajax:
$.ajax({
url: ‘/api/categories/${catID}/items/addItem/’,
type: “POST”,
contentType: “application/json”,
etc

Now I get a problem from the url and cannot read the value of ${catID}

This is the real API: “/api/categories/{categoryId}/items/addItem”
I have looked inside the site but the solution does not work with me.
Now how can I pass a value of a variable if the variable are in the middle.

Thanks in advance.

How to&Answer:

You’re trying to use template literals which require backticks(`) instead of single quotes(‘).

url: `/api/categories/${catID}/items/addItem/`