当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 关于 js Promise 中如何取到 [[PromiseValue]] 值,SyntaxError: Unexpected token < in JSON at position 0

关于 js Promise 中如何取到 [[PromiseValue]] 值,SyntaxError: Unexpected token < in JSON at position 0

2020年07月17日  | 移动技术网IT编程  | 我要评论

在这里插入图片描述
话不多说,直接上代码:

fetch('/api',{
            content_type:"application/json",
        })
        // 主要代码
        .then(response => response.json())
        // 拿值
        .then(data => {
            console.log(data)
        })
        .catch(err => {
            console.log(err)
        })

这样就可以了。在这里插入图片描述
但是第二个请求接口就报错,这个接口postman测没问题,浏览器看也没问题,可请求就报了这个问题:
SyntaxError: Unexpected token < in JSON at position 0
如何解决?
其实就是把代理的路径改成带有http请求的就没有问题了,接口正常访问到

//这是有错代码,把“/page”/改成http路径的就可以了
fetch('/page',{
        content_type:"application/json",
 })

正确代码:

fetch('http://localhost:3000/page/',{
     content_type:"application/json",
 })
 .then(response => response.json())
 .then(res => {
     console.log(res)
 })
 .catch(err => {
     console.log(err)
 })
}, 0);

成功:
在这里插入图片描述

本文地址:https://blog.csdn.net/rock_23/article/details/107372912

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网