程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)?

开发过程中遇到JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)的问题如何解决?下面主要结合日常开发的经验,给出你关于JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)的解决方法建议,希望对你解决JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)有所启发或帮助; @H_618_2@我尝试从服务器获取信息,但如果想解析答案时出错。

@H_618_2@我的代码:

const data = ["10093"]

const options = {
    auth: userpsw,method: 'GET',CURLOPT_POSTFIELDS: data
};

const request = http.request(url,options,function(responsE) {
  response.on("data",function(data){
    console.log(JsON.parse(data));
  })
})
@H_618_2@答案:

undefined:1
<br />
^

SyntaxError: Unexpected token < in JsON at position 0
    at JsON.parse (<anonymous>)
@H_618_2@如果我不解析数据:

const request = http.request(url,function(data){
    console.log(data);
@H_618_2@然后我得到以下按摩:

Buffer 3c 62 72 20 2f 3e 0a 3c 62 3e 57 61 72 6e 69 6e 67 3c 2f 62 3e 3a 20 20 69 6d 70 6c 6f 64 65 28 29 3a 20 49 6e 76 61 6c 69 64 20 61 72 67 75 6d 65 6e ... 282 more bytes
@H_618_2@我看不到什么?如何阅读答案?

解决方法

@H_618_2@我建议使用请求模块而不是 http :

var options = {
    hostname: '_your_url',port: 'your_port',path: '/your_path',method: 'GET',json:true
}
request(options,function(error,response,body){
    if(error) console.log(error);
    else console.log(body);
});
, @H_618_2@您的响应不是 JSON 格式,这就是您尝试解析响应时出错的原因,使用 try 和 catch 处理错误。

大佬总结

以上是大佬教程为你收集整理的JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)全部内容,希望文章能够帮你解决JSON Parse = > SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。