jsp标签在JavaScript中使用时,可能会出现的一个问题。

直接上代码

 1 <script type="text/javascript">
 2     var E = window.wangEditor;
 3     var editor = new E('#editor');
 4     // 或者 var editor = new E( document.getElementById('editor') )
 5     editor.create();
 6     $(function () {
 7         $("#btn1").click(function () {
 8             if(${not empty requestScope.article.articleId}){
 9                 var article = {
10                     "articleId":${requestScope.article.articleId},
11                     "articleContent":editor.txt.html(),
12                     "articleTitle":htmlEncode($("#title").val()),
13                     "writerId":${sessionScope.blogger.bloggerId},
14                     "articleCreateTime":getNowTime(),
15                     "classifyName": $("#classify").val()
16                 }
17             }else{
18                 var article = {
19                     "articleContent":editor.txt.html(),
20                     "articleTitle":htmlEncode($("#title").val()),
21                     "writerId":${sessionScope.blogger.bloggerId},
22                     "articleCreateTime":getNowTime(),
23                     "classifyName": $("#classify").val()
24                 }
25             }
26 
27             $.ajax({
28                 type:"post",
29                 url:"${ctx}/article/save",
30                 data:JSON.stringify(article),
31                 contentType:"application/json;charset=utf-8",
32                 success:function (data) {
33                     if(data != ""){
34                         alert("保存成功!");
35                         window.location="${ctx}/blogger/backstage";
36                     }else{
37                         alert("保存失败!");
38                     }
39                 },
40                 error:function () {
41                     alert("连接失败");
42                 }
43             })
44         });
45     })
46  </script>

虽然逻辑是对的,但是运行的时候会报错。因为javaScript在解释运行的时候,${requestScope.article.articleId}如果不存在,那么黄色部分会变成:

 "articleId": ,

会报错。那么如何解决这一问题呢,想到了一个办法:

 "articleId":${requestScope.article.articleId}+"",

这样,不管 ${requestScope.article.articleId}存在不存在,就都没有问题了。

猜你喜欢

转载自www.cnblogs.com/phdeblog/p/10634142.html