javascript 从文件中读取json数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6483493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading json data from a file
提问by chinmay
I am running a server with Go programming language, and when I load the server in the browser, the temp handler function is called and the getjson.html file is served by this temp Handler function. Now the screen shows a "Get Json Data" button. On clicking this button, I am not getting any results (as something should be displayed on the screen).
我正在使用 Go 编程语言运行服务器,当我在浏览器中加载服务器时,会调用临时处理程序函数,并且该临时处理程序函数为 getjson.html 文件提供服务。现在屏幕显示“获取 Json 数据”按钮。单击此按钮时,我没有得到任何结果(因为屏幕上应该显示某些内容)。
I checked the javascript console and there are no errors as such. I am not able to figure out what the problem is, why isn't there any output on the screen.
我检查了 javascript 控制台,没有错误。我无法弄清楚问题是什么,为什么屏幕上没有任何输出。
Contents of servejson.go :
servejson.go 的内容:
package main
import (
"http"
"flag"
)
var path = flag.String("root", "/home/chinmay/work/json/getjson.html", "Set root directory, use absolute path")
func temp(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "text/html")
http.ServeFile(w,r,*path)
}
func main(){
http.HandleFunc("/",temp)
http.ListenAndServe(":8080", nil)
}
Contents of getjson.html :
getjson.html 的内容:
package main
import (
"http"
"flag"
)
var path = flag.String("root", "/home/chinmay/work/json/getjson.html", "Set root directory, use absolute path")
func temp(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "text/html")
http.ServeFile(w,r,*path)
}
func main(){
http.HandleFunc("/",temp)
http.ListenAndServe(":8080", nil)
}
Contents of json_data.js:
json_data.js 的内容:
{
"firstName": "John",
"lastName": "Doe",
"age": 25
}
回答by T.J. Crowder
Yes, you can. Live example.Provided that json.txt
is a resource next to the document in which this code is running, and (on some browsers) provided this is not running from a local file (e.g., a file://
URL rather than an http://
one; some browsers are okay with local files accessing other local files via ajax, others are not).
是的你可以。活生生的例子。假设它json.txt
是运行此代码的文档旁边的资源,并且(在某些浏览器上)假设它不是从本地文件(例如,file://
URL 而不是 URL http://
;某些浏览器可以访问本地文件访问其他文件)本地文件通过ajax,其他人不是)。
A couple of notes:
一些注意事项:
In the
$("div").append(field + " ");
line,
field
will be the valueof each property (e.g., "John").- The order in which the properties are listed is completely undefined.
在里面
$("div").append(field + " ");
行,
field
将是每个属性的值(例如,“John”)。- 列出属性的顺序是完全未定义的。
So for this specificexample, you'd probably be better off with
所以对于这个特定的例子,你可能会更好
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.getJSON("json.txt",function(result){
$("div").append(result.firstName + " " + result.lastName + " " + result.age);
});
});
});
</script>
Update: From your comments on another answer, it seems like you might be unclear on where and how the script code is running. The JavaScript script runs on the client's browser. The path to use to reference json.txt
is exactly like (say) the path to an image you want to show on a page. json.txt
must be accessible via the web server, just like an image would need to be accessible via the web server. Think of json.txt
as just another resource used by your web page, like any other. In terms of the path, and how you have to make json.txt
available, the same rules apply. To be clear: Script running client-side in a web page cannot access a server-side file that can't be retrieved by the browser.
更新:从您对另一个答案的评论来看,您似乎不清楚脚本代码的运行位置和方式。JavaScript 脚本在客户端的浏览器上运行。用于引用json.txt
的路径与(例如)您要在页面上显示的图像的路径完全相同。json.txt
必须可以通过网络服务器访问,就像图像需要通过网络服务器访问一样。将其json.txt
视为网页使用的另一种资源,就像其他任何资源一样。在路径以及您必须如何json.txt
提供方面,相同的规则适用。需要明确的是:在网页中运行客户端的脚本无法访问浏览器无法检索的服务器端文件。
Update 2: You've posted more code, and it looks like you've made your server onlyserve the getjson.html
file. Your server alsohas to serve the json.txt
file, or the browser can't access it.
更新 2:您发布了更多代码,看起来您的服务器仅提供getjson.html
文件服务。您的服务器还必须提供该json.txt
文件,否则浏览器将无法访问它。
回答by Le Droid
Sorry if this isn't related to Go, but I falt here while searching a solution to a similar problem in JQuery and the solution isn't related to the tool used.
抱歉,如果这与 Go 无关,但我在 JQuery 中搜索类似问题的解决方案时遇到了问题,并且该解决方案与所使用的工具无关。
I got a problem today to simulate a Json query on a program linked to a database, without the database. So I saved a Json result to a file and wanted to use it instead. The json.txt file was on a server and viewable in the browser but $.getJSON was trowing an error.
我今天遇到了一个问题,在没有数据库的情况下,在链接到数据库的程序上模拟 Json 查询。所以我将一个 Json 结果保存到一个文件中,并想改用它。json.txt 文件位于服务器上,可以在浏览器中查看,但 $.getJSON 出现错误。
To solve this problem, all I had to do is to rename the file with an "html" extention, like json.html and to remove the header in the file. i.e. You need to NOT have "Content-Type: application/json" in the begining of your text file.
为了解决这个问题,我所要做的就是用“html”扩展名重命名文件,比如 json.html 并删除文件中的标题。即您不需要在文本文件的开头有“内容类型:应用程序/json”。
Could be specific to a web server (Apache) or browser security (Firefox) but at least it worked.
可能特定于 Web 服务器 (Apache) 或浏览器安全性 (Firefox),但至少它有效。
回答by Kevin Bowersox
$.getJSON() needs a URL as a parameter. You are trying to access a file from the directory structure of the local machine/server which is not possible in JS.
$.getJSON() 需要一个 URL 作为参数。您正在尝试从本地机器/服务器的目录结构中访问文件,这在 JS 中是不可能的。
See the jquery documentation http://api.jquery.com/jQuery.getJSON/
请参阅 jquery 文档http://api.jquery.com/jQuery.getJSON/