返回Json数据浏览器带上<pre></pre>标签解决方法

本文介绍了解决上传文件时返回JSON被预格式化的问题,包括两种解决方式:一是通过前端使用正则表达式处理返回的JSON数据;二是修改后台返回类型为text/html。

问题:  当后台获取到前台传来的文件时(例如上传功能, 导入功能), 返回类型为application/json, 这个时候响应到前端的JSON格式的数据格式可能是:

<pre style="word-wrap: break-word; white-space: pre-wrap;">{"JsonKey":"JsonValue"}</pre>

这个是不同浏览器对返回数据处理的问题。

 

 

 

解决方法:

 

  

 

  1. 在前端解决

 

 

 

var reg = /<pre.+?>(.+)<\/pre>/g;  
var result = data.match(reg);  
data = RegExp.$1;

data就是后台返回前端的数据, 用正则表达式处理一下即可。


 

  2. 在后台解决

将返回的类型从application/json改为text/html
这个解决方法因为每个系统后台采用Struts或者SpringMVC不同可能处理有点不一样在此就不举例了。

 

 

 

上传File时,浏览器总是添加<pre>的解决办法

 

       在使用Structs的FileUpload组件进行上传文件的时候,在返回的JSon字符串里面总是莫名其妙的添加了<pre>标签,例 如返回内容为"{\"message\":\"导入文件已成功\",\"result\":\"OK\"}",在浏览器里面就变成了"<pre style="word-wrap: break-word; white-space: pre-wrap;">"{\"message\":\"导入文件已成功\",\"result\":\"OK \"}"</pre>",这样就导致Ajax解析返回值的时候出现错误。

         我的上传代码,默认总是进入到error的处理中。

         

复制代码
 jQuery.ajaxFileUpload({
            type:"POST",
            url:"/****/upload.action?file="+fileName,
            secureuri:false,
            fileElementId:"upload", dataType: "json", success:function (data, status) { if (data != null) { data = JSON.parse(data); MBJ.alert("提示", data.message); } else { MBJ.alert("提示", "上传文件出错, 服务器返回错误信息: status = " + status); } }, error:function (data, status, e) { MBJ.alert("提示", "上传文件出错: status = " + status); } });
复制代码

      谷歌了一圈之后,发现很多人和我是一样的,解决办法都是把response的返回类型设置为【text/html】。在Struts2中需要在Action的配置中这样设置。

  

< result name="success" type="json">
                 < param name="root">msg</ param >
                 < param name="contentType">text/html;charset=UTF-8</ param >
             </ result >

     如果用的是annotation的方式的话,需要加上这句话

 @Action(value = "upload", results = { @Result(name = SUCCESS, params = { "root", "msg", "contentType",
            "text/html;charset=UTF-8" }, type = "json") })

 

 

转载于:https://www.cnblogs.com/Rozdy/p/4994214.html

文档是一个接口的返回结果,如何判断是否返回与下面的字符一致<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n <meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n <meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\">\n <meta content=\"yes\" name=\"apple-mobile-web-app-capable\">\n <meta content=\"black\" name=\"apple-mobile-web-app-status-bar-style\">\n <meta content=\"telephone=no\" name=\"format-detection\">\n <meta content=\"email=no\" name=\"format-detection\">\n <meta name=\"report\" content='{\"pid\": \"blog\", \"spm\":\"1001.2101\"}'>\n <meta name=\"applicable-device\" content=\"mobile\">\n <meta http-equiv=\"Cache-Control\" content=\"no-transform\" />\n <meta http-equiv=\"Cache-Control\" content=\"no-siteapp\" />\n <meta name=\"csdn-baidu-search\" content='{\"autorun\":true,\"install\":true,\"keyword\":\"富文本test\"}'>\n <meta name=\"referrer\" content=\"always\">\n <title>富文本test - 优快云博客</title>\n <link href=\"https://g.csdnimg.cn/static/logo/favicon32.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />\n <link rel=\"stylesheet\" href=\"https://csdnimg.cn/release/blogv2/dist/app/css/app_detail_enter-055a9826b5.min.css\"/>\n <style>\n .MathJax_Preview{\n display: none !important;\n }\n </style>\n <script>\n initpage();\n window.onresize = initpage;\n function initpage(){\n var view_width = document.getElementsByTagName('html')[0].getBoundingClientRect().width;\n var _html = document.getElementsByTagName('html')[0];\n window.fontSize = view_width>750?750/7.5:view_width/7.5;\n _html.style.fontSize= window.fontSize+'px';\n }\n </script>\n <script>\n var isBlackSkin = false;\n var isApp = true;\n window.csdn = window.csdn ? window.csdn : {};\n window.csdn.toolbarIsBlack = isBlackSkin\n var username = \"cpongo13\";\n var blogShopInIt = true;\n var blogStaticHost = \"https://csdnimg.cn/release/blogv2/\";\n var articleId = \"151363106\";\n var blogUrl = \"https://blog.youkuaiyun.com/\";\n //baidu js \n var blogTitle = \"富文本test\";\n var baiduKey = \"富文本test\";\n\n var needInsertBaidu = false;\n var recommendRegularDomainArr = [\"blog.youkuaiyun.com/.+/article/details/\",\"download.youkuaiyun.com/download/\",\"edu.youkuaiyun.com/course/detail/\",\"ask.youkuaiyun.com/questions/\",\"bbs.youkuaiyun.com/topics/\",\"www.youkuaiyun.com/gather_.+/\"];\n var currentUrl = \"https://blog.youkuaiyun.com/cpongo13/article/details/151363106\";\n var baiduCount = 0;\n var isBaiduPre = false;\n var highlight = [\"test\",\"富文本\"];\n var baiduSearchType = \"title\";\n var articleSource = 1;\n //baidujs\n var apprewad = false;\n var blogShopVersion = '';\n var baiduSearchChannel = 'app_relevant'\n var baiduSearchIdentification = ''\n var distRequestId = '1757490097863_05439'\n var recommendListArr = []\n window.nextArticleUrl = ''\n recommendListArr.push('https://blog.youkuaiyun.com/weixin_28759829/article/details/112906049')\n recommendListArr.push('https://blog.youkuaiyun.com/weixin_42561476/article/details/117916767')\n recommendListArr.push('https://blog.youkuaiyun.com/ZH445063477/article/details/125165435')\n recommendListArr.push('https://blog.youkuaiyun.com/weixin_31100705/article/details/113153212')\n recommendListArr.push('https://blog.youkuaiyun.com/weixin_39836726/article/details/114211678')\n var isEnterprise = false;\n if (recommendListArr.length) {\n if (recommendListArr.length >= 5) {\n window.nextArticleUrl = recommendListArr[parseInt(Math.random() * 5)]\n } else{\n window.nextArticleUrl = recommendListArr[parseInt(Math.random() * recommendListArr.length)]\n }\n }\n var privateData = [];\n var privateEduData = [];\n var isGitCodeBlog = false;\n var isOpenSourceBlog = false;\n </script>\n</head>\n<body class=\"\">\n <!-- flowchart 箭头图标 勿删 -->\n <svg xmlns=\"http://www.w3.org/2000/svg\" style=\"display: none;\"><path stroke-linecap=\"round\" d=\"M5,0 0,2.5 5,5z\" id=\"raphael-marker-block\" style=\"-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\"></path></svg>\n <div class=\"article_content \">\n <link rel=\"stylesheet\" href=\"https://csdnimg.cn/release/blogv2/dist/mobile/css/edit_views_html-db03dd5f8c.min.css\"/>\n <link rel=\"stylesheet\" href=\"https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-10bf609291.css\" />\n <div id=\"content_views\" class=\"htmledit_views edit_views clearfix \">\n <div class=\"article_top_msg_box\">\n </div>\n <h4>富文本test</h4> \n<p>以下是一篇包含多种富文本功能的测试文章,可用于验证富文本编辑器的各项功能。</p> \n<hr /> \n<p><strong>标题示例:富文本功能测试</strong></p> \n<p>斜体文本<br /> <strong>粗体文本</strong><br /> <strong>粗斜体文本</strong><br /> <s>删除线文本</s><br /> <u>下划线文本</u><br /> <code>行内代码</code></p> \n<hr /> \n<h4>段落与对齐</h4> \n<p>左对齐段落:<br /> 这是一个普通的左对齐段落,用于测试富文本的基本段落功能。</p> \n<p>居中段落:<br /> <p style="text-align:center">这是一个居中对齐的段落,通常用于标题或强调内容。</p></p> \n<p>右对齐段落:<br /> <p style="text-align:right">这是一个右对齐的段落,适合签名或日期。</p></p> \n<hr /> \n<h4>列表功能</h4> \n<p>无序列表:</p> \n<ul><li>项目一</li><li>项目二 \n <ul><li>子项目一</li><li>子项目二</li></ul> </li><li>项目三</li></ul> \n<p>有序列表:</p> \n<ol><li>第一项</li><li>第二项 \n <ol><li>嵌套项</li><li>嵌套项</li></ol> </li><li>第三项</li></ol> \n<p>任务列表:</p> \n<ul><li>[x] 已完成任务</li><li>[ ] 未完成任务</li><li>[ ] 另一个任务</li></ul> \n<hr /> \n<h4>链接与图片</h4> \n<p><span style=\"outline-color:currentcolor; outline-style:none\" tabindex=\"-1\"><a href=\"https://example.com/\" target=\"_blank\" title=\"这是一个超链接示例\" rel=\"nofollow noopener noreferrer\">这是一个超链接示例</a></span></p> \n<p><img src=\"https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fvia.placeholder.com%2F150&pos_id=HuY5V0Vi\" /></p> \n<hr /> \n<h4>表格功能</h4> \n<table><thead><tr><th>列1</th><th>列2</th><th>列3</th></tr></thead><tbody><tr><td>单元格1</td><td>单元格2</td><td>单元格3</td></tr><tr><td>斜体</td><td><strong>粗体</strong></td><td><s>删除线</s></td></tr><tr><td>左对齐</td><td>居中</td><td>右对齐</td></tr></tbody></table> \n<hr /> \n<h4>代码块</h4> \n<pre>\n </pre> \n<p> </p> \n<pre>\n </pre> \n<p><img src=\"data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==\" /></p> \n<p> </p> \n<hr /> \n<h4>引用与分割线</h4> \n<blockquote> \n <p> </p> \n</blockquote> \n<hr /> \n<h4>嵌入内容</h4> \n<p><iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe></p> \n<hr /> \n<h4>数学公式</h4> \n<p>行内公式:$E=mc^2$</p> \n<p>独立公式: $$ \\sum_{i=1}^n i = \\frac{n(n+1)}{2} $$</p> \n<hr /> \n<h4>特殊字符与HTML</h4> \n<p>HTML实体:© ™ ®</p> \n<p>直接嵌入HTML: <span style="color:red">红色文本</span></p> \n<hr /> \n<h4>其他格式</h4> \n<p>上标示例:x<sup>2</sup><br /> 下标示例:H<sub>2</sub>O</p> \n<p>高亮文本:<mark>高亮内容</mark></p> \n<hr /> \n<p>这篇测试文章涵盖了富文本编辑器的主要功能,包括文本样式、段落、列表、表格、代码、嵌入内容等,可用于全面验证富文本支持的完整性。
最新发布
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值