我是新来JSONP
,下面是我的情况:
我用PHP
写一个js文件与JSON
内容前端阅读,我想通过以下方式和它的作品。
但是,我想问
A)是有必要包括jsoncallback()
来包装album.js
的JSON
内容?
B)使用$.getScript()
来读js文件是否包含跨域的JSON内容很好吗?
由于
1.代码来加载JS文件
$.getScript("http://127.0.0.1:8080/album.js")
function jsoncallback (data){
$.each(data, function() {
console.log($(this))
})
}
2. album.js内容
jsoncallback([{
"id": "23",
"author": "roy",
"email": "a",
"age": "0",
"tel": "1",
"title": "test",
"image_name": "Lighthouse.jpg",
"image_type": "image\/jpeg",
"image_size": "561276",
"status": "1",
"create_date": "2013-04-03 14:39:32",
"modify_date": "2013-04-09 19:25:07"
}, {
"id": "22",
"author": "roy",
"email": "a",
"age": "0",
"tel": "1",
"title": "test",
"image_name": "Desert.jpg",
"image_type": "image\/jpeg",
"image_size": "845941",
"status": "1",
"create_date": "2013-04-03 14:39:29",
"modify_date": "2013-04-09 19:25:02"
}
])
它是确定,但最好是通过回调函数作为参数的名称,让你的代码重用能力。如果你使用jquery,我会使用包含的jsonp功能,因为它有一个超时选项和每个请求回调的可能性。 – 2013-04-11 05:05:27