liuooooo
V2EX  ›  问与答

ajax 访问本地 json 时提示跨域

  •  
  •   liuooooo · Oct 26, 2018 · 1929 views
    This topic created in 2805 days ago, the information mentioned may be changed or developed.

    本来是在 webstorm 中运行的 demo,用 vscode 打开用时不能访问本地 json 文件,报错提示跨域
    最后了解是浏览器的安全机制,想按照 csdn 中的一个解决方法,https://blog.csdn.net/jiaoshuaiai/article/details/78417683, 使用 jsonp 跨域解决试试
    json 文件却一直提示 Uncaught SyntaxError: Unexpected token :
    最后在 json 内容的最外边加上[],不报错了但是也没得到 json 文件内容
    看很多文章中 json 文件的格式都是:indexDemo({ "a":"lllll", "b":"2222" }),这种格式是什么?
    大家有没有遇到这种情况。。

    3 replies    2018-10-26 17:07:53 +08:00
    liuooooo
        1
    liuooooo  
    OP
       Oct 26, 2018
    发现启动 vscode 的 go live 就可以访问本地 json,开启了一个本地服务器
    virusdefender
        2
    virusdefender  
       Oct 26, 2018
    那是 jsonp
    liuooooo
        3
    liuooooo  
    OP
       Oct 26, 2018
    @virusdefender 嗯嗯,解决了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   985 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:27 · PVG 06:27 · LAX 15:27 · JFK 18:27
    ♥ Do have faith in what you're doing.