How to pass variable in request body?

时间:2016-04-04 16:32:09

标签: javascript jquery node.js

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.

2 个答案:

答案 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 });