I want to pass one variable in request body.But whole body needs to go as a string.
for example :
var xyz = "abc";
var requestBody = '{"name":"xyz"}';
How can I pass the variable value in request body.Whole request body needs to go as a string.Complete string has one variable.
答案 0 :(得分:2)
Use template strings.
var requestBody = `{"name":"${xyz}"}`;
答案 1 :(得分:2)
Or instead of building a JSON string manually, make an object and call JSON.stringify()
on it:
var requestBody = JSON.stringify({ name: xyz });