【问题标题】:Ionic 3, encodeURI with firebase离子 3,encodeURI 与 firebase
【发布时间】:2018-07-22 14:08:03
【问题描述】:

我需要向具有这种形状的 firebase 发送一个 http 请求

https://db.firebaseio.com/0.json?&orderBy="name"&startAt=query&endAt=query+"\uf8ff"&limitToLast=1&print=pretty&auth=auth_token

我的问题是,当我调用此请求时,控制台中出现 400 错误,因为它将 %22 替换为问号和 \uf8ff 的其他符号,我认为 firebase 无法识别。

    let name = '"name"';
    let cod = '"\uf8ff"';
    let url = ('https://db.firebaseio.com/0.json?&orderBy=' + encodeURIComponent(name) + '&startAt=' + encodeURIComponent(birraName) + '&endAt=' + encodeURIComponent(birraName) + '+' + encodeURIComponent(cod) + '&limitToLast=1&print=pretty&auth=' + encodeURIComponent(this.idToken));
    let response = this.http.get(url).map(res => res.json());
    return response;

然后在控制台中

400 错误请求

你有什么想法吗?

【问题讨论】:

    标签: javascript firebase ionic-framework firebase-realtime-database ionic3


    【解决方案1】:

    您的目标字符串中缺少引号来标记字符串值。如果您要搜索以 Marco 开头的节点,则应为 https://db.firebaseio.com/0.json?&orderBy="name"&startAt="Marco"&endAt="Marco\uf8ff"&limitToLast=1&print=pretty&auth=auth_token。请注意 "Marco""Marco\uf8ff" 周围的双引号。

    在您的 JavaScript 中构建这些:

    var url = 'https://db.firebaseio.com/0.json';
    url = url + '?orderBy="' + encodeURIComponent(name) + '"';
    url = url + "&startAt="' + encodeURIComponent(birraName) + '"';
    url = url + "&endAt="' + encodeURIComponent(birraName) + '\uf8ff"';
    url = url + '&limitToLast=1&print=pretty';
    url = url + '&auth="' + encodeURIComponent(this.idToken))+'"";
    

    模板文字也可能有助于保持可读性:

    let url = (`https://db.firebaseio.com/0.json?orderBy=${encodeURIComponent(name)}&startAt=${encodeURIComponent(birraName)}&endAt=${encodeURIComponent(birraName)}${encodeURIComponent(cod)}&limitToLast=1&print=pretty&auth=${encodeURIComponent(this.idToken))}`;
    

    【讨论】:

    • 谢谢你,但现在我有一个 401 未经授权的“错误”:“验证签名失败。”。但是我检索了用户令牌并将其放在代码末尾的“this.idToken”中
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 2018-05-07
    • 2016-07-04
    • 2019-08-16
    • 1970-01-01
    • 2017-10-19
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多