【问题标题】:Javascript tags not working in HTMLJavascript 标记在 HTML 中不起作用
【发布时间】:2018-03-02 08:28:13
【问题描述】:

我编写了一个 API 来发送电子邮件。我为此使用邮递员。它适用于普通文本。在“HTML”中添加脚本标签时,会抛出以下错误。

400 错误请求

带有响应正文

SyntaxError: Unexpected token
  在 Object.parse (本机)
  解析时 (/root/node_modules/body-parser/lib/types/json.js:88:17)
在 /root/node_modules/body-parser/lib/read.js:116:18
在调用回调 (/root/node_modules/body-parser/node_modules/raw-body/index.js:262:16)
完成时 (/root/node_modules/body-parser/node_modules/raw-body/index.js:251:7)
在 IncomingMessage.onEnd (/root/node_modules/body-parser/node_modules/raw-body/index.js:307:7)
在 IncomingMessage.EventEmitter.emit (events.js:92:17)
  在 _stream_readable.js:920:16
在 process._tickDomainCallback (node.js:459:13)

我发送的请求正文是:

{
    "from": "\"David Johnson\" <djohnson@gmail.com>",
    "to": "david.johnson@gmail.com",
    "subject": "Test",
    "html": "Dear David, <br /> <br /> Thanks for the mail. PFB the detailed report.
    <script src=\"https://code.highcharts.com/highcharts.js\"></script>
    <script src=\"https://code.highcharts.com/modules/exporting.js\"></script>

    <div id=\"container\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>
    <script>
    Highcharts.chart('container',
    {
    chart: {
        type: 'areaspline'
        },
    title: {
        text: 'Bandwidth Utilization'
        },
    legend: {
        layout: 'vertical',
        align: 'left',
        verticalAlign: 'top',
        x: 150,
        y: 100,
        floating: true,
        borderWidth: 1,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
    xAxis: {
        categories: [
        '00: 00',
        '01: 00',
        '02: 00',
        '03: 00',
        '04: 00',
        '05: 00',
        '06: 00',
        '07: 00',
        '08: 00',
        '09: 00',
        '10: 00',
        '11: 00',
        '12: 00',
        '13: 00',
        '14: 00',
        '15: 00',
        '16: 00',
        '17: 00',
        '18: 00',
        '19: 00',
        '20: 00',
        '21: 00',
        '22: 00',
        '23: 00'
            ],
        title: {
        text: 'Hours (GMT)'
            }
        },
    yAxis: {
        title: {
        text: 'Bandwidth (Mbps)'
            }
        },
    tooltip: {
        shared: true,
        valueSuffix: ' Mbps'
        },
    credits: {
        enabled: false
        },
    plotOptions: {
        areaspline: {
        fillOpacity: 0.5
            }
        },
    series: [
            {
        name: 'India - (GMT +5.5)',
        data: [
                    3,
                    5,
                    4,
                    10,
                    12,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    3,
                    4,
                    3
                ]
            },
            {
        name: 'Ireland - (GMT +0.0)',
        data: [
                    1,
                    3,
                    4,
                    3,
                    3,
                    5,
                    4,
                    4,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
                ]
            },
            {
        name: 'Samoa - (GMT -11.0)',
        data: [
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    3,
                    4,
                    4,
                    9,
                    15,
                    10,
                    12,
                    0,
                    0,
                    0,
                    0
                ]
            }
        ]
    });
    </script>"
}

【问题讨论】:

    标签: javascript html email postman


    【解决方案1】:

    JSON 不允许 换行

    根据问题multiline-strings-in-json 中的解决方案,您可以将所有内容写在一行中,但如果换行符很重要,您可以使用\n,或者将多个字符串包装成一个字符串数组。

    它与&lt;script&gt; 标记无关,因为标记位于字符串内部,并且字符串中的每个字符都是有效的,只要您在需要时将它们转义,例如 qoutes (\") 或换行符 (@ 987654325@)

    使用\n

    {
        "html":"Dear David, <br /> <br /> Thanks for the mail. PFB the detailed report.\n<script src=\"https://code.highcharts.com/highcharts.js\"></script>\n...
    }
    

    使用数组作为包装器

    "html": [
        "Dear David, <br /> <br /> Thanks for the mail. PFB the detailed report.",
        "<script src=\"https://code.highcharts.com/highcharts.js\"></script>",
        "<script src=\"https://code.highcharts.com/modules/exporting.js\"></script>",
    
        "<div id=\"container\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>",
        "<script>",
        "Highcharts.chart('container', {",
        "chart: {"
    ]
    

    【讨论】:

      【解决方案2】:

      据我所知,请求没问题,只是您可能错过了使用 [AllowHtml] 标头设置的模型属性。

      请参考这个看看它是否有效: 例如:

      public class MyViewModel
      {
        [AllowHtml]
        public string SomeHtmlProperty { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-04-22
        • 2017-06-10
        • 2013-10-05
        • 2015-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多