2017-09-21 19 views
2

私は電子メールを送信するためのAPIを書いています。私はこれにPostmanを使用しています。通常のテキストで正常に動作しています。 "HTML"にスクリプトタグを追加すると、次のエラーが発生します。レスポンスボディJavaScriptで動作しないJavaScriptタグが

SyntaxError: Unexpected token
   at Object.parse (native)
   at parse (/root/node_modules/body-parser/lib/types/json.js:88:17)
   at /root/node_modules/body-parser/lib/read.js:116:18
   at invokeCallback (/root/node_modules/body-parser/node_modules/raw-body/index.js:262:16)
   at done (/root/node_modules/body-parser/node_modules/raw-body/index.js:251:7)
   at IncomingMessage.onEnd (/root/node_modules/body-parser/node_modules/raw-body/index.js:307:7)
   at IncomingMessage.EventEmitter.emit (events.js:92:17)
   at _stream_readable.js:920:16
   at process._tickDomainCallback (node.js:459:13)

400 Bad Request

私が送りますリクエストボディです:JSONはに基づいて改行

を許可していませんでした

{ 
    "from": "\"David Johnson\" <[email protected]>", 
    "to": "[email protected]", 
    "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>" 
} 

答えて

3

あなたがすべてを書くことができる質問のソリューションmultiline-strings-in-json私はn改行が重要な場合は、\nを使用するか、複数の文字列を文字列の配列に折り返します。

タグが文字列内で、文字列内のすべての文字がqoutes(\")または改行(\n

のように、必要に応じて、あなたがそれらをエスケープ限り有効ですので <script>タグとは何の関係もありません

使用\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: {" 
] 
+0

おかげで、@Roman。それは正常に動作しています。 :) –

0

限りラッパーとしてアレイを使用私はあなたのモデルプロパティを[AllowHtml]ヘッダーで設定されていない可能性があります。それが動作するかどうか、このreffer

PLSのは、以下を参照してください。 例:答えを

public class MyViewModel 
{ 
    [AllowHtml] 
    public string SomeHtmlProperty { get; set; } 
} 
関連する問題