英文:
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代码:
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, 与用户ID相关的JSON文件)
}
我知道这段代码是错误的,但我完全被困住了,不知道如何将用户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:
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);
}
The HTML
<core-ajax id="load-trial" method="GET"></core-ajax>
And the backend in go(just the relevant part):
func handleloadTrials(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
//retrieve trials
http.ServeFile(w, r, JSON file related to user id)
}
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 格式的。
英文:
<core-ajax url="{{url}}" handleAs="json" params='{"userID": "{{userID}}"}'></core-ajax>
The parameter should be on JSON format.
答案2
得分: 0
我以这种方式部分解决了问题
Javascript:
loadTrials : function loadTrials() {
var _this = this,
load = this.shadowRoot.querySelector('#load-trial');
//将userID传递给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>
Go:
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,需要帮助吗?谢谢
英文:
I partially solved in this way
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>
Go:
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
}
But now I have the Encoded Json file which I'm not sure how to send back to the javascript, any help? Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论