Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中的POST请求URL中传递变量?(How do I pass a variable inside a POST request URL in JavaScript?)

I am trying to pass a variable that is derived from a database (and keeps changing) to a fetch URL.

(我试图将派生自数据库的变量(并不断更改)传递给获取URL。)

I don't want to hard code it in the URL.

(我不想在URL中对其进行硬编码。)

How do I go about it?

(我该怎么办?)

Here is the snippet of the code.

(这是代码片段。)

  };
  var searchString = amount;
  var payload = JSON.stringify(data);
  var options = {
    'method': 'POST',
    'Content-Type': 'application/json', 
    'payload' : data,
  };
  var url = 'https://......./**amount**/budget_items?token';
  var response = UrlFetchApp.fetch(url, options);

I want to pass the search string variable but I don't know-how.

(我想传递搜索字符串变量,但我不知道如何做。)

Any help?

(有什么帮助吗?)

  ask by levi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to include searchString in the url you can use string concatenation or string interpolation.

(如果要在url包括searchString ,则可以使用字符串串联或字符串插值。)

Here is string concatenation:

(这是字符串连接:)

var url = 'https://.../' + searchString + '/...';

And here is string interpolation:

(这是字符串插值:)

var url = `https://.../${searchString}/...`;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...