json解析html失败,【报Bug】JSON.parse解析json字符串错误

本文探讨了在HTML+环境中,使用JavaScript的XMLHttpRequest获取远程JSON数据时遇到的解析错误,对比了不同浏览器的行为,并提供了问题解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

详细问题描述

[内容]

在html+的环境里,JSON.parse无法解析正常的json字符串

{

var xhr=new plus.net.XMLHttpRequest();

xhr.open("GET","http://mao.s1.natapp.cc/index.php/IWanMao/Home/getcontent/t/f/pageindex/1");

xhr.onreadystatechange=function(){

if(xhr.readyState==4&&xhr.status==200){

var res=xhr.responseText; //res为:‘{"eid":"397"}’

console.log(typeof res);

console.log(res);

var abc=JSON.parse(res);

console.log(abc);

console.log(abc.eid);

}

}

xhr.send();

}

document.addEventListener("plusready",myready,false);

解析不出来,报错:

string at index.html:27

{"eid":"397"} at index.html:28

SyntaxError: Unexpected token  in JSON at position 0

string at index.html:27

{"eid":"397"} at index.html:28

SyntaxError: Unexpected token  in JSON at position 0

在谷歌浏览器,用的js自带的xmlhttprequest,函数一样,正常,结果如下:

string

asa.html:20 {"eid":"397"}

asa.html:22 Object {eid: "397"}

asa.html:23 397

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值