drpph80800 2011-06-26 10:57
浏览 107
已采纳

从文件读取json数据

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
}
  • 写回答

3条回答 默认 最新

  • drfm55597 2011-06-26 11:02
    关注

    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>
    

    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 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条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭