项目场景:

在将ES的数据发送到前端的过程中出现了数据无法展示的情况。


问题描述

前端无法展示数据,并出现乱码。后端,发出警告但是没有报错。


原因分析:

查看后端的警告,发现没什么问题,在看前端的数据,发现也是正常的。问题应该出在前端数据转换上。

05-31 19:43:04.504 [http-nio-8080-exec-1] INFO  o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
05-31 19:43:04.504 [http-nio-8080-exec-1] INFO  org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
05-31 19:43:04.506 [http-nio-8080-exec-1] INFO  org.springframework.web.servlet.DispatcherServlet - Completed initialization in 2 ms
05-31 19:43:05.687 [http-nio-8080-exec-4] WARN  org.elasticsearch.client.RestClient - request [POST http://120.48.46.177:9200/hotel/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true] returned 2 warnings: [299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]
05-31 19:43:05.706 [http-nio-8080-exec-5] WARN  org.elasticsearch.client.RestClient - request [POST http://120.48.46.177:9200/hotel/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true] returned 2 warnings: [299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]
05-31 19:43:10.673 [http-nio-8080-exec-7] WARN  org.elasticsearch.client.RestClient - request [POST http://120.48.46.177:9200/hotel/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true] returned 2 warnings: [299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]
05-31 19:43:10.675 [http-nio-8080-exec-6] WARN  org.elasticsearch.client.RestClient - request [POST http://120.48.46.177:9200/hotel/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true] returned 2 warnings: [299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.16.3-4e6e4eab2297e949ec994e688dad46290d018022 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]

查看前端的报错信息,前端提示json转换失败,提示错误的代码在383行。

SyntaxError: Unexpected token u in JSON at position 1

分析提示报错信息的代码:打印错误的地方在catch哪里,数据走catch表示then里面的代码错误了。于是乎分别在各个比较重要的地方进行打印操作。前端再次运行发现 console.log(3)这个语句无法进入,确定问题在if语句块内。

        axios.post("/hotel/list", params).then(resp => {this.hotels = resp.data.hotels;this.total = resp.data.total;console.log(1)this.totalPage = Math.floor((this.total + 5 - 1) / 5);console.log(2)if (location) {this.setMapCenter(location);} else if(this.hotels && this.hotels.length > 0){this.setMapCenter(this.hotels[0].location);}this.initMarker();console.log(3)}).catch(err => {console.log(err)this.hotels = []this.total = 271;this.totalPage = 28;})},

if语句块最终跳转到下面这个方法中,最终发现了问题所在。我在把数据插入ES中的时候location这个属性是不加空格的,而前端在解析json的时候是加空格的,最终导致json无法解析。因此在进行数据插入时要保持数据一致,不然很容易错。找这个错误花了我两个小时的时间,就是为了一个空格。


解决方案:

前端解析时删去空格。

    location(loc) {let arr = loc.split(",");let json = '[' + arr[1] + ',' + arr[0] + ']'return JSON.parse(json);},

SyntaxError: Unexpected token u in JSON at position 1相关推荐

  1. 报错,Error in created hook: “SyntaxError: Unexpected token o in JSON at position 1“

    1.问题 [Vue warn]: Error in created hook: "SyntaxError: Unexpected token o in JSON at position 1& ...

  2. 报错SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>)的解决方法

    报错SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse ()的解决方法 刚刚开始学习node.js会报很多各种各样的 ...

  3. 93.<报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)的解决方法>

    @[TOC]( 报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse ()的解决方法) 报错代码: const ...

  4. 报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)的解决方法

    报错 SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse ()的解决方法 报错代码: let articles = ...

  5. SyntaxError: Unexpected token o in JSON at position 1 JSON.parse (<anonymous>)

    VM651 WAService.js:2 SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () 解决方案 查看J ...

  6. 微信小程序 SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>)

    源代码 function loadAD(){wx.request({url: 'https://wx.xxx.cm/Classes/AD/AD.json',success(res){var jsond ...

  7. Error in created hook: “SyntaxError: Unexpected token u in JSON at position 0“

    Error in created hook: "SyntaxError: Unexpected token u in JSON at position 0" 这个错误是由于使用JS ...

  8. Uncaught SyntaxError: Unexpected token E in JSON at position 0 at JSON.parse (anonymous)

    当直接将返回值JSON.parse(),并且使用这个值去取值,报这个Uncaught SyntaxError: Unexpected token E in JSON at position 0 at ...

  9. 解决:Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse

    后端已经正常返回了数据: 前端报错:Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (< ...

最新文章

  1. ASP.NET MVC 中将FormCollection与实体间转换方法
  2. url中携带中文乱码问题
  3. 记录我的学习历程--二维数组解决平面图形题
  4. python 爬取大乐透开奖结果
  5. shell 中 $(( )) 与 $( ) 还有 ${ } 的区别
  6. template might not exist or might not be accessible by any of the configured
  7. golang操作postgresql数据库jsonb类型
  8. win11菜单栏的推荐项目怎么取消 windows11取消推荐项目的设置方法
  9. qq空间显示手机型号android,qq说说显示手机型号 qq说说显示手机型号在哪里设置...
  10. Barrett And Montgomery of Polynomials
  11. 翻译: Revisiting Contrastive Methods for Unsupervised Learning of Visual Representations
  12. 网银--U盾等--支付宝等总结
  13. coreldraw x7 分布_了解CorelDRAW X7工作界面
  14. 基于MATLAB/Simulink的Buck变换器仿真模型,包含开环控制和闭环控制两种控制
  15. 查看电脑操作系统版本
  16. pycharmSDK is not defined for Run Configuration 解决
  17. Mac Pro 8g java 开发_2017款MacBook Pro开发java,8g内存够用吗?
  18. JAVA面试-基础加强与巩固:反射、注解、泛型等
  19. android webview 找不到网页,webview loadUrl 显示“找不到网页”
  20. Dockerfile创建Wordpress论坛

热门文章

  1. debezium集成Oralce攻略(上)
  2. 3d模型导入MAYA常见错误!
  3. 业务层Service的作用
  4. 【J2EE】模仿天猫商城(后台篇)
  5. 京东与淘宝孰优孰劣?
  6. GIS应用技巧之环形地图制作
  7. 小程序性能优化——文件的本地存储10M优化算法
  8. 计算机七进制乘法,编程达人
  9. html 自动生产,HTML5 Three.js 3D人偶玩具自动化生产车间动画
  10. PrimeNG之FileUpload