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

2015-01-21 20:48:40 +08:00
 14ly

类似于把

{"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中获取。
对于只有静态的网页帮助很大

2403 次点击
所在节点    问与答
5 条回复
mcfog
2015-01-21 20:53:01 +08:00
为什么不jsonp?
14ly
2015-01-21 21:12:28 +08:00
@mcfog 已感谢。边用边学的中学生表示,虽然见过这个,但是不知道是可以跨域的。
takatost
2015-01-21 21:12:38 +08:00
jsonp
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
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)
})

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/164270

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX