将用户ID传递给core-ajax方法的GET请求,以从Google Cloud存储中检索数据。

huangapple go评论132阅读模式
英文:

Pass user ID to core-ajax method GET to retrieve data in Google Cloud storage

问题

我有一个在Google引擎上使用Go后端的应用程序。我正在尝试检索之前保存在Google云存储中的JSON文件。后端基于Polymer和JavaScript。问题是需要使用用户ID通过core-ajax调用来检索数据。

以下是我目前正在使用的JavaScript代码:

  1. loadTrials: function loadTrials() {
  2. var _this = this,
  3. load = this.shadowRoot.querySelector('#load-trial');
  4. load.url = "http://url/loadTrials";
  5. load.go();
  6. load.addEventListener('core-response', function(ev) {
  7. var json = ev.detail.response;
  8. _this.trialData = JSON.parse(json)['trial-data'];
  9. console.log(JSON.parse(json)['trial-data'])
  10. }, false);
  11. }

HTML部分:

  1. <core-ajax id="load-trial" method="GET"></core-ajax>

Go后端代码(只包含相关部分):

  1. func handleloadTrials(w http.ResponseWriter, r *http.Request) {
  2. defer r.Body.Close()
  3. //retrieve trials
  4. http.ServeFile(w, r, 与用户ID相关的JSON文件)
  5. }

我知道这段代码是错误的,但我完全被困住了,不知道如何将用户ID传递给GET方法,然后使用该ID从云存储中检索特定的文件(它是一个JSON文件),然后将此JSON文件传递给Go中的http.ServeFile方法。

需要帮助吗?非常感谢。

英文:

I have an app in google engine with a go backend. I'm trying to retrieve a json file that I previously saved in google cloud storage. The backend is based on polymer and javascript. The problem is that the data need to be retrieved using the user-id through a core-ajax call.

Here is the javascript code I'm using at the moment:

  1. loadTrials : function loadTrials() {
  2. var _this = this,
  3. load = this.shadowRoot.querySelector(&#39;#load-trial&#39;);
  4. load.url=&quot;http://url/loadTrials&quot;;
  5. load.go();
  6. load.addEventListener(&#39;core-response&#39;, function(ev) {
  7. var json = ev.detail.response;
  8. _this.trialData = JSON.parse(json)[&#39;trial-data&#39;];
  9. console.log(JSON.parse(json)[&#39;trial-data&#39;])
  10. }, false);
  11. }

The HTML

  1. &lt;core-ajax id=&quot;load-trial&quot; method=&quot;GET&quot;&gt;&lt;/core-ajax&gt;

And the backend in go(just the relevant part):

  1. func handleloadTrials(w http.ResponseWriter, r *http.Request) {
  2. defer r.Body.Close()
  3. //retrieve trials
  4. http.ServeFile(w, r, JSON file related to user id)
  5. }

I know this code is wrong, but I'm completely stuck and I don't know how to pass the user ID to the GET method, and then use the ID to retrieve the specific file on cloud storage (it's a json file) and then pass this Json file to the http.ServeFile method in Go.

Any help?
Thanks a lot.

答案1

得分: 1

<core-ajax url="{{url}}" handleAs="json" params='{"userID": "{{userID}}"}'></core-ajax>

参数应该是 JSON 格式的。

英文:
  1. &lt;core-ajax url=&quot;{{url}}&quot; handleAs=&quot;json&quot; params=&#39;{&quot;userID&quot;: &quot;{{userID}}&quot;}&#39;&gt;&lt;/core-ajax&gt;

The parameter should be on JSON format.

答案2

得分: 0

我以这种方式部分解决了问题

Javascript:

  1. loadTrials : function loadTrials() {
  2. var _this = this,
  3. load = this.shadowRoot.querySelector('#load-trial');
  4. //将userID传递给core-ajax
  5. _this.userID = _this.app.userSession.user;
  6. load.url="http://url/loadTrials";
  7. load.go();
  8. load.addEventListener('core-response', function(ev) {
  9. var json = ev.detail.response;
  10. _this.trialData = JSON.parse(json)['trial-data'];
  11. console.log(JSON.parse(json)['trial-data'])
  12. }, false);
  13. }

HTML:

  1. <core-ajax id="load-trial" params="{{userID}}" method="GET"></core-ajax>

Go:

  1. type trialStruct []struct {
  2. Trial struct {
  3. Index int `json:"index"`
  4. Word string `json:"word"`
  5. WordDisplayTime int `json:"wordDisplayTime"`
  6. AnswerMaxTime int `json:"answerMaxTime"`
  7. FixationTime int `json:"fixationTime"`
  8. Train bool `json:"train"`
  9. Test bool `json:"test"`
  10. Grammatical bool `json:"grammatical"`
  11. Grammar string `json:"grammar"`
  12. keyboard bool `json:"keyboard"`
  13. }
  14. Metadata struct {
  15. }
  16. }
  17. func (d *saveData) readFile(fileName string) trialStruct {
  18. trialName := fileName + "/trials"
  19. rc, err := storage.NewReader(d.ctx, bucket, trialName)
  20. if err != nil {
  21. d.errorf("readFile: unable to open file from bucket %q, file %q: %v", bucket, trialName, err)
  22. }
  23. defer rc.Close()
  24. slurp, err := ioutil.ReadAll(rc)
  25. if err != nil {
  26. d.errorf("readFile: unable to read data from bucket %q, file %q: %v", bucket, fileName, err)
  27. }
  28. var trialJson trialStruct
  29. err1 := json.Unmarshal(slurp, &trialJson)
  30. if err != nil {
  31. log.Printf("error:", err1)
  32. }
  33. return trialJson
  34. }

但现在我有一个编码的Json文件,我不确定如何将其发送回Javascript,需要帮助吗?谢谢

英文:

I partially solved in this way

Javascript:

  1. loadTrials : function loadTrials() {
  2. var _this = this,
  3. load = this.shadowRoot.querySelector(&#39;#load-trial&#39;);
  4. //pass userID to core-ajax
  5. _this.userID = _this.app.userSession.user;
  6. load.url=&quot;http://url/loadTrials&quot;;
  7. load.go();
  8. load.addEventListener(&#39;core-response&#39;, function(ev) {
  9. var json = ev.detail.response;
  10. _this.trialData = JSON.parse(json)[&#39;trial-data&#39;];
  11. console.log(JSON.parse(json)[&#39;trial-data&#39;])
  12. }, false);
  13. }

HTML:

  1. &lt;core-ajax id=&quot;load-trial&quot; params=&quot;{{userID}}&quot; method=&quot;GET&quot;&gt;&lt;/core-ajax&gt;

Go:

  1. type trialStruct []struct {
  2. Trial struct {
  3. Index int `json:&quot;index&quot;`
  4. Word string `json:&quot;word&quot;`
  5. WordDisplayTime int `json:&quot;wordDisplayTime&quot;`
  6. AnswerMaxTime int `json:&quot;answerMaxTime&quot;`
  7. FixationTime int `json:&quot;fixationTime&quot;`
  8. Train bool `json:&quot;train&quot;`
  9. Test bool `json:&quot;test&quot;`
  10. Grammatical bool `json:&quot;grammatical&quot;`
  11. Grammar string `json:&quot;grammar&quot;`
  12. keyboard bool `json:&quot;keyboard&quot;`
  13. }
  14. Metadata struct {
  15. }
  16. }
  17. func (d *saveData) readFile(fileName string) trialStruct {
  18. trialName := fileName + &quot;/trials&quot;
  19. rc, err := storage.NewReader(d.ctx, bucket, trialName)
  20. if err != nil {
  21. d.errorf(&quot;readFile: unable to open file from bucket %q, file %q: %v&quot;, bucket, trialName, err)
  22. }
  23. defer rc.Close()
  24. slurp, err := ioutil.ReadAll(rc)
  25. if err != nil {
  26. d.errorf(&quot;readFile: unable to read data from bucket %q, file %q: %v&quot;, bucket, fileName, err)
  27. }
  28. var trialJson trialStruct
  29. err1 := json.Unmarshal(slurp, &amp;trialJson)
  30. if err != nil {
  31. log.Printf(&quot;error:&quot;, err1)
  32. }
  33. return trialJson
  34. }

But now I have the Encoded Json file which I'm not sure how to send back to the javascript, any help? Thanks

huangapple
  • 本文由 发表于 2015年5月19日 03:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/30311770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定