从文件中读取json数据
我正在使用 Go 编程语言运行服务器,当我在浏览器中加载服务器时,会调用临时处理函数,并且 getjson.html 文件由该临时处理函数提供服务。现在屏幕显示“获取 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 的内容:
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)
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你可以。 实例。 假设
json.txt
是该文档旁边的资源代码正在运行,并且(在某些浏览器上)前提是该代码不是从本地文件运行(例如,file://
URL 而不是http://
; 有些浏览器可以通过 ajax 本地文件访问其他本地文件,而其他浏览器则可以 不是)。一些注释:
在
line,
field
将是每个属性(例如“John”)的值。因此,对于这个具体示例,您可能最好使用
实时示例
更新:从您对另一个答案的评论来看,您似乎不清楚脚本代码在何处以及如何运行。 JavaScript 脚本在客户端的浏览器上运行。用于引用
json.txt
的路径与您要在页面上显示的图像的路径完全相同。json.txt
必须可以通过网络服务器访问,就像图像需要通过网络服务器访问一样。将json.txt
视为网页使用的另一种资源,就像任何其他资源一样。就路径以及如何使json.txt
可用而言,适用相同的规则。需要明确的是:在网页中运行客户端的脚本无法访问浏览器无法检索的服务器端文件。更新 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., afile://
URL rather than anhttp://
one; some browsers are okay with local files accessing other local files via ajax, others are not).A couple of notes:
In the
line,
field
will be the value of each property (e.g., "John").So for this specific example, you'd probably be better off with
Live example
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 ofjson.txt
as just another resource used by your web page, like any other. In terms of the path, and how you have to makejson.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 thejson.txt
file, or the browser can't access it.抱歉,如果这与 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.
$.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/