V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
14ly
V2EX  ›  问与答

我觉得应该有这样的服务,但是找不到,所以来问一下:有没有直接把 ajax 才能读取的 json 转换成可以直接引用在网页中的的?

  •  1
     
  •   14ly · 2015-01-21 20:48:40 +08:00 · 2423 次点击
    这是一个创建于 3618 天前的主题,其中的信息可能已经有所发展或是发生改变。

    类似于把

    {"programmers":[{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
    {"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
    {"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
    ],
    "authors":[
    {"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"},
    {"firstName":"Tad","lastName":"Williams","genre":"fantasy"},
    {"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"}
    ],
    "musicians":[
    {"firstName":"Eric","lastName":"Clapton","instrument":"guitar"},
    {"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"}
    ]}

    变成这种的

    var people={"programmers":[{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
    {"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
    {"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
    ],
    "authors":[
    {"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"},
    {"firstName":"Tad","lastName":"Williams","genre":"fantasy"},
    {"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"}
    ],
    "musicians":[
    {"firstName":"Eric","lastName":"Clapton","instrument":"guitar"},
    {"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"}
    ]}

    --

    这样就不需要考虑跨域问题,直接在dom里引用,然后在js中获取。
    对于只有静态的网页帮助很大

    mcfog
        1
    mcfog  
       2015-01-21 20:53:01 +08:00 via Android   ❤️ 1
    为什么不jsonp?
    14ly
        2
    14ly  
    OP
       2015-01-21 21:12:28 +08:00
    @mcfog 已感谢。边用边学的中学生表示,虽然见过这个,但是不知道是可以跨域的。
    takatost
        3
    takatost  
       2015-01-21 21:12:38 +08:00
    jsonp
    luoweihua7sync
        4
    luoweihua7sync  
       2015-01-21 21:14:59 +08:00
    var str="{a:1,b:2}";
    var json=(new Function("return "+str))();
    console.log(json);

    要么就用JSONP
    14ly
        5
    14ly  
    OP
       2015-01-21 21:50:22 +08:00
    jsonp需要服务器支持,不能达到我的要求,但是找到了“jsonp代理”,可用。
    附上我修改后的雅虎代理使用:
    src = 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'

    $.getJSON("http://query.yahooapis.com/v1/public/yql", {
    q: 'select * from json where url="' + src + '"',
    format: "json"
    },
    function(data) {
    console.log(data.query.results.json.images)
    })
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5275 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 07:47 · PVG 15:47 · LAX 23:47 · JFK 02:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.