如何在 POST 消息中保留换行符

当我收到一封电子邮件的内容时,它的格式是换行符,但当我将它发送到 API 时,结果是没有空格的文本。


有什么办法可以保持间距?


编辑:


const postNewTask = (taskTitle, taskComment, workerId) => {

  const { projectId, tasklistId } = dataStore();


  const { userEmail, apiKey } = credentialsStore();


  const options = {

    method: 'POST',

    headers: {

      Authorization:

        'Basic ' + Utilities.base64Encode(`${userEmail}:${apiKey}`),

    },

  };


  if (taskComment && workerId) {

    options.payload = JSON.stringify({

      name: taskTitle,

      comment: {

        content: taskComment,

      },

      worker: parseInt(workerId),

    });

  }


慕村225694
浏览 136回答 1
1回答

烙印99

我解决了。我只需要使用taskComment.replace(/(?:\r\n|\r|\n)/g, '<br>'),API 就知道该怎么做。最终工作代码:const postNewTask = (taskTitle, taskComment, workerId) => {&nbsp; const { projectId, tasklistId } = dataStore();&nbsp; const { userEmail, apiKey } = credentialsStore();&nbsp; const options = {&nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; Authorization:&nbsp; &nbsp; &nbsp; &nbsp; 'Basic ' + Utilities.base64Encode(`${userEmail}:${apiKey}`),&nbsp; &nbsp; },&nbsp; };&nbsp; if (taskComment && workerId) {&nbsp; &nbsp; options.payload = JSON.stringify({&nbsp; &nbsp; &nbsp; name: taskTitle,&nbsp; &nbsp; &nbsp; comment: {&nbsp; &nbsp; &nbsp; &nbsp; content: taskComment.replace(/(?:\r\n|\r|\n)/g, '<br>'),&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; worker: parseInt(workerId),&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript