简体   繁体   English

Javascript:如何处理响应字符串解析中的语法错误

[英]Javascript: How to handle syntaxError in response string parsing

In my HTML file, I have a single line (below) that gets a response from a device over WiFi & makes data available to my JavaScript, it works well except when there is an error in the response text & it halts.在我的 HTML 文件中,我有一行(下方)通过 WiFi 从设备获取响应并使数据可用于我的 JavaScript,它运行良好,除非响应文本中出现错误并停止。

<script type="text/javascript" src="http://192.168.4.1/"></script>

The response is a text string representing JavaScript variables, it sometimes gets corrupted throwing an error.响应是一个表示 JavaScript 变量的文本字符串,它有时会被破坏并引发错误。

Examples:例子:

Uncaught SyntaxError: invalid assignment left-hand side  192.168.4.1:14:3  
      SyntaxError: unterminated string literal  192.168.4.1:3:8

I need a solution to capture the error of parsing the received string so I can run the above statement again to get a new string.我需要一个解决方案来捕获解析接收到的字符串的错误,以便我可以再次运行上述语句以获取新字符串。

Thanks谢谢

below is a picture of the response string (JS variables.)下面是响应字符串的图片(JS变量。)

在此处输入图片说明

The issue is that once the script is loaded, its content might be misformed and I would like to avoid having errors due to that.问题是一旦加载脚本,其内容可能会出现错误,我希望避免因此而出现错误。

Update #1.更新#1。 get the data with XMLHttpRequest使用 XMLHttpRequest 获取数据

<script>
   const Http = new XMLHttpRequest();
   const url='http://192.168.4.1/';
    Http.open("GET", url);
  //  Http.setRequestHeader('Access-Control-Allow-Origin', '*');
    Http.send();
  ReturnVar = Http.responseText
  Http.onreadystatechange = (e) => { console.log(Http.responseText) }
</script>

Below is the detail in browser Console, Headers after script above run.以下是浏览器控制台中的详细信息,上面运行脚本后的标题。

   GET
    scheme  http
    host        192.168.4.1
    filename    /
    Address 192.168.4.1:80
    Transferred 1.82 KB (1.82 KB size
Request headers (278B)  
GET / 
Host: 192.168.4.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: null
DNT: 1
Connection: keep-alive
Cache-Control: max-age=0

You have a script tag, which is loading the script and evaluating its content.您有一个脚本标签,它正在加载脚本并评估其内容。 You need to programmatically modify this, so you need to您需要以编程方式修改它,因此您需要

Get the file as a text以文本形式获取文件

https://www.freecodecamp.org/news/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa/ https://www.freecodecamp.org/news/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa/

You just need to programmatically download the content the file, not as the src of a script .您只需要以编程方式下载文件的内容,而不是作为scriptsrc

Evaluate it评估一下

Before you proceed, read this: https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/在继续之前,请阅读: https : //javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

Now, that you know that eval is not exactly popular - and rightly so - and if you are still sure you need to run a script as it is, study eval and make it work equivalently as it was现在,您知道eval并不完全流行 - 确实如此 - 如果您仍然确定需要按原样运行脚本,请研究eval并使其按原样运行

Encapsulate it into try-catch封装成try-catch

Example for error:错误示例:

eval("'");

Example for error in try-catch: try-catch 中的错误示例:

try {eval("'")} catch (ex) {}

you can call the address using ajax, and then if the response is in json you can easily use it, but if its some sort of script or something else, wrap it in a script tag an append it to the body.您可以使用ajax调用地址,然后如果响应是在json中,您可以轻松使用它,但是如果它是某种脚本或其他东西,请将其包装在脚本标记中并将其附加到正文中。 assume the result is what you got from ajax request and you have jQuery in your project:假设result是您从 ajax 请求中得到的,并且您的项目中有 jQuery:

$('body').append('<script>'+result+'</script>')

in this case you can wrap your code in try catch and handle the errors在这种情况下,您可以将代码包装在 try catch 中并处理错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM