【问题标题】:Pass user ID to core-ajax method GET to retrieve data in Google Cloud storage将用户 ID 传递给 core-ajax 方法 GET 以检索 Google Cloud 存储中的数据
【发布时间】:2015-07-30 11:07:15
【问题描述】:

我在谷歌引擎中有一个带有 go 后端的应用程序。我正在尝试检索我之前保存在谷歌云存储中的 json 文件。后端基于聚合物和javascript。问题是需要通过 core-ajax 调用使用 user-id 来检索数据。

这是我目前使用的 javascript 代码:

loadTrials : function loadTrials() {
            var _this = this,
                load = this.shadowRoot.querySelector('#load-trial');

            load.url="http://url/loadTrials";
            load.go();
            load.addEventListener('core-response', function(ev) {
                var json = ev.detail.response;
                _this.trialData = JSON.parse(json)['trial-data'];
                console.log(JSON.parse(json)['trial-data'])
            }, false);
        }

HTML

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

Go 中的后端(只是相关部分):

func handleloadTrials(w http.ResponseWriter, r *http.Request) {

    defer r.Body.Close()

    //retrieve trials
    http.ServeFile(w, r, JSON file related to user id)

}

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

有什么帮助吗? 非常感谢。

【问题讨论】:

    标签: javascript json google-app-engine go polymer


    【解决方案1】:
    <core-ajax url="{{url}}" handleAs="json" params='{"userID": "{{userID}}"}'></core-ajax>
    

    参数应为 JSON 格式。

    【讨论】:

      【解决方案2】:

      我这样部分解决了

      Javascript:

      loadTrials : function loadTrials() {
                  var _this = this,
                      load = this.shadowRoot.querySelector('#load-trial');
                  //pass userID to core-ajax
                  _this.userID = _this.app.userSession.user;
                  load.url="http://url/loadTrials";
                  load.go();
                  load.addEventListener('core-response', function(ev) {
                      var json = ev.detail.response;
                      _this.trialData = JSON.parse(json)['trial-data'];
                      console.log(JSON.parse(json)['trial-data'])
                  }, false);
              }
      

      HTML:

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

      去:

      type trialStruct []struct {
          Trial struct {
              Index           int    `json:"index"`
              Word            string `json:"word"`
              WordDisplayTime int    `json:"wordDisplayTime"`
              AnswerMaxTime   int    `json:"answerMaxTime"`
              FixationTime    int    `json:"fixationTime"`
              Train           bool   `json:"train"`
              Test            bool   `json:"test"`
              Grammatical     bool   `json:"grammatical"`
              Grammar         string `json:"grammar"`
              keyboard        bool   `json:"keyboard"`
          }
          Metadata struct {
          }
      }
      
      func (d *saveData) readFile(fileName string) trialStruct {
          trialName := fileName + "/trials"
          rc, err := storage.NewReader(d.ctx, bucket, trialName)
          if err != nil {
              d.errorf("readFile: unable to open file from bucket %q, file %q: %v", bucket, trialName, err)
      
          }
          defer rc.Close()
          slurp, err := ioutil.ReadAll(rc)
          if err != nil {
              d.errorf("readFile: unable to read data from bucket %q, file %q: %v", bucket, fileName, err)
          }
          var trialJson trialStruct
          err1 := json.Unmarshal(slurp, &trialJson)
          if err != nil {
              log.Printf("error:", err1)
          }
          return trialJson
      }
      

      但是现在我有编码的 Json 文件,我不确定如何将其发送回 javascript,有什么帮助吗?谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        相关资源
        最近更新 更多