ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 6.5 JavaScript常见问题 ### 6.5.1 在线调试 #### 在线调试地址 [http://www.runoob.com/try/try.php?filename=tryjs\_events](http://www.runoob.com/try/try.php?filename=tryjs_events) #### 在线调试案例 ``` function test(){ var name="张三"; var age=17; var result = ""; if(age < 18){ result = name + " 你还未成年,不能观看"; }else{ result = name + "欢迎观看"; } return result; } document.write(test()); ``` 打开在线调试地址,在 **源代码**中输入: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ruyi.ai </title> <script> function test() { var name = "张三"; var age = 17; var result = ""; if (age < 18) { result = name + " 你还未成年,不能观看"; } else { result = name + "欢迎观看"; } return result; } document.write(test()); </script> </head> <body> </body> </html> ``` 提交运行后,可以看到运行结果: ![![Alt text](./4-3-1.png)](images/4-3-1.png) ### 6.5.2 常见问题 #### 查名人李白的返回json: ``` { "_text": "名人李白", "msg_id": "226a653e-bc9a-4fc1-8a78-b74124723a94", "intents": [ { "parameters": { "person": "李白", "key": "ccd34f0ff4cbc9770c568b6d4066998a515b7de2" }, "action": "先祖名人-旧版", "name": "先祖名人_查名人信息", "result": { "code": 200, "response": [ { "@id": "http://data.library.sh.cn/jp/entity/person/464z3akcx8e97hnp", "@type": "http://www.library.sh.cn/ontology/Person", "label": [ { "@language": "cht", "@value": "李白" }, { "@language": "chs", "@value": "李白" } ], "childOf": "http://data.library.sh.cn/jp/entity/person/7lxb2uo35t5hhll7", "spouseOf": "http://data.library.sh.cn/jp/entity/person/l33x3r4rkr939uhd", "birthday": "乾隆辛巳年二月十七日辰時", "courtesyName": "萬同", "deathday": "道光庚寅年正月二十九日酉時", "description": "朝檜公長子字萬同生於乾隆辛巳年二月十七日辰時卒於道光庚寅年正月二十九日酉時遷居繁昌縣黄單橋卒葬其地\r\n配劉氏生於乾隆壬午年十月十三日酉時卒於道光乙酉年五月二十二日酉時偕夫同遷同葬\r\n生子三文佩文道文綺", "genealogyName": "白", "orderOfSeniority": "1", "relatedWork": "http://data.library.sh.cn/jp/resource/work/kkgv5g289866rbm4", "roleOfFamily": "http://data.library.sh.cn/jp/vocab/ancestor/xian-zu", "familyName": "http://data.library.sh.cn/authority/familyname/h5ce5gtiilgtrjz5", "@context": { "roleOfFamily": { "@id": "http://www.library.sh.cn/ontology/roleOfFamily", "@type": "@id" }, "familyName": { "@id": "http://xmlns.com/foaf/0.1/familyName", "@type": "@id" }, "description": "http://www.library.sh.cn/ontology/description", "spouseOf": { "@id": "http://purl.org/vocab/relationship/spouseOf", "@type": "@id" }, "courtesyName": "http://www.library.sh.cn/ontology/courtesyName", "label": "http://bibframe.org/vocab/label", "birthday": "http://www.library.sh.cn/ontology/birthday", "relatedWork": { "@id": "http://www.library.sh.cn/ontology/relatedWork", "@type": "@id" }, "deathday": "http://www.library.sh.cn/ontology/deathday", "childOf": { "@id": "http://purl.org/vocab/relationship/childOf", "@type": "@id" }, "genealogyName": "http://www.library.sh.cn/ontology/genealogyName", "orderOfSeniority": "http://www.library.sh.cn/ontology/orderOfSeniority" } } ], "text": "李白,字萬同", "type": "dialog" }, "outputs": [ { "type": "wechat.text", "property": { "text": "李白,字萬同" } } ], "score": "1.0", "scoreColor": "c4", "is_match": 1, "id": "8d76f8e4-a51f-4c78-9b30-c05f06da3164" } ], "meta_process_milliseconds": 6024 } ``` 如上的json的返回字段中有@等特殊符号,对于引用特殊符号的处理方式是: ``` var mingzi = sys['out']['response'][0]['label'][0]['@value']; var dynasty = sys['out']['response'][0]['temporalValue']; var zi = sys['out']['response'][0]['courtesyName']; ``` 查询名人或许会存在接口没返回的情况,这是会报错undefined,对于这种情况要预先定义下,处理方式是: ``` var result = ""; var result = sys.out.response; if (result == "未查询到数据...") { return "我找不到这个名人呢"; } else { result = sys.out.response.result; } ``` 对于调用接口存在的404,408的类似code的报错,处理方式是: ``` if (code == 404) { return "没有找到你要的资源"; } else if (code == 408) { return "刚才网络不好,请您再说一次"; } ```