英文:
reading json data from a file
问题
我正在使用Go编程语言运行一个服务器,当我在浏览器中加载服务器时,会调用temp处理函数,并且getjson.html文件由这个temp处理函数提供服务。现在屏幕上显示一个“获取Json数据”的按钮。点击此按钮后,我没有得到任何结果(屏幕上应该显示一些内容)。
我检查了javascript控制台,没有任何错误。
我无法弄清楚问题在哪里,为什么屏幕上没有任何输出。
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)
}
getjson.html的内容:
<html>
<head>
<script src="json_data.js"></script>
</head>
<body>
<button onclick="getData()">Get Json Data</button>
<script>
function getData(){
console.log(jsonData);
}
</script>
</body>
</html>
json_data.js的内容:
{
"firstName": "John",
"lastName": "Doe",
"age": 25
}
英文:
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).
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.
Contents of 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 :
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:
{
"firstName": "John",
"lastName": "Doe",
"age": 25
}
答案1
得分: 0
是的,你可以。只要json.txt
是在运行此代码的文档旁边的资源,并且(在某些浏览器上)提供的不是从本地文件运行的(例如,file://
URL而不是http://
URL;某些浏览器允许本地文件通过ajax访问其他本地文件,而其他浏览器则不允许)。
一些注意事项:
-
在
$("div").append(field + " ");
这一行中,
field
将是每个属性的值(例如,"John")。 -
列出属性的顺序是完全未定义的。
所以对于这个特定的例子,你可能最好使用:
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.getJSON("json.txt",function(result){
$("div").append(result.firstName + " " + result.lastName + " " + result.age);
});
});
});
</script>
更新:根据您在另一个答案中的评论,似乎您可能不清楚脚本代码在何处以及如何运行。JavaScript脚本在客户端的浏览器上运行。用于引用json.txt
的路径与(例如)要在页面上显示的图像的路径完全相同。json.txt
必须通过Web服务器访问,就像图像需要通过Web服务器访问一样。将json.txt
视为您的网页使用的另一个资源,就像任何其他资源一样。在路径和如何使json.txt
可用的方面,适用相同的规则。明确一点:在Web页面上运行的客户端端脚本无法访问浏览器无法检索的服务器端文件。
更新2:您发布了更多代码,看起来您已经让服务器仅提供getjson.html
文件。您的服务器还必须提供json.txt
文件,否则浏览器无法访问它。
英文:
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).
A couple of notes:
-
In the
$("div").append(field + " ");
line,
field
will be the value of each property (e.g., "John"). -
The order in which the properties are listed is completely undefined.
So for this specific example, 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.
Update 2: You've posted more code, and it looks like you've made your server only serve the getjson.html
file. Your server also has to serve the json.txt
file, or the browser can't access it.
答案2
得分: 0
抱歉,如果这与Go无关,但我在搜索JQuery中类似问题的解决方案时遇到了这个问题,而解决方案与所使用的工具无关。
今天我遇到了一个问题,需要在与数据库连接的程序上模拟一个Json查询,但没有数据库。所以我将一个Json结果保存到一个文件中,并希望使用它。json.txt文件在服务器上可见,并且在浏览器中可以查看,但是$.getJSON却报错了。
为了解决这个问题,我所要做的就是将文件重命名为"html"扩展名,比如json.html,并且删除文件中的头部。也就是说,你的文本文件开头不能有"Content-Type: application/json"。
可能与Web服务器(Apache)或浏览器安全性(Firefox)有关,但至少它起作用了。
英文:
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.
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.
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.
Could be specific to a web server (Apache) or browser security (Firefox) but at least it worked.
答案3
得分: -1
$.getJSON()需要一个URL作为参数。你正在尝试访问本地机器/服务器的目录结构中的文件,这在JS中是不可能的。
请参阅jQuery文档http://api.jquery.com/jQuery.getJSON/
英文:
$.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.
See the jquery documentation http://api.jquery.com/jQuery.getJSON/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论