2016-04-01 15 views
0

Bu bir mareşal veya unmarshal meselesi değil, JQuery'imle birlikte yazara yazmam gereken nedir?Golang'da json verilerini bir sayfaya nasıl sunarım?

Şu anda git

func serveJson(w http.ResponseWriter, r *http.Request, ps httprouter.Params){ 
    j, _ := os.Open("adjs.json") 

    defer j.Close() 

    var obj respWords 

    json.NewDecoder(j).Decode(&obj) 
    js, _ := json.Marshal(obj) 

    w.Header().Set("Content-Type", "application/json") 

    w.Write(js) 
} 

için bu var ve bu json alma benim jquery olduğunu

<script type = "text/javascript"> 
    function randomize() { 
     scene = {} 
     $.getJSON('adjs.json', function(scene){ 
      console.log('encoding') 
     }) 
     console.log(scene) 
    } 
</script> 

cevap

3

, dış değişkene verileri kaydetmek de mantık yapmaz senin zaman uyumsuz isteği ajax isteğinizin başarılı işlevi

$.getJSON('adjs.json', function(scene){ 
      console.log(scene); 
      //do logic here with scene 
     }); 
1

JSON içeren bir dosyaya hizmet verebilirsiniz Aşağıdakileri düzeltmek ve kodlamak gerekmez.

Ayrıca ServeFile kullanabilirsiniz:

func serveJson(w http.ResponseWriter, r *http.Request, ps httprouter.Params){ 
    http.ServeFile(w, r, "adjs.json") 
} 
İlgili konular