【问题标题】:Pinterest posting not working inside inappbrowserPinterest 发布在 inappbrowser 中不起作用
【发布时间】:2016-07-12 13:54:28
【问题描述】:

我正在尝试使用 inappbrowser 将一些文本和图像发布到 Pinterest,但它会抛出错误“参数'image_url'(值 http:null)不是有效的 URL 格式。”

这里是示例代码。

var pinterestUrl = "http://www.pinterest.com/pin/create/button/";
     pinterestUrl += "?url=https://www.google.co.in/";
     pinterestUrl += "&media=http://www.google.co.ma/images/srpr/logo1w.png";
     pinterestUrl += "&description=Text Description";   
var pinterest = window.open(pinterestUrl, '_blank');

如果我将代码从“_blank”更改为“_system”,它在网络浏览器和系统浏览器(iPhone/Android)中运行良好。我尝试使用eventListener“loadstart”检查url,发现inappbrowser会自动添加一些不必要的额外参数,如“创建下一个按钮”。

任何建议都会有所帮助。

【问题讨论】:

  • 您是否尝试过在每个查询参数上使用encodeURIComponent()?必须对查询参数进行编码,以便特殊字符不会弄乱 url 解析。
  • 感谢 Zack 的建议。我已经尝试过使用 encodeURIComponent 但在对 inappbrower 进行编码后无法加载 URL 并抛出错误“webView:didFailLoadWithError - 在此服务器上找不到请求的 URL”
  • 介意分享尝试中的代码吗?不编码很可能是问题所在,但您可能编码错误。
  • var encodedURL = encodeURIComponent(pinterestUrl); var pinterest = window.open(encodedURL, '_blank');如果我在这里做错了什么,请告诉我。

标签: cordova pinterest inappbrowser


【解决方案1】:

根据我们在 cmets 中的对话,我认为问题在于没有对您的查询参数进行编码。 encodeURIComponent 方法用于在查询中使用时对特殊字符进行编码。对整个 url 进行编码将不起作用,因为所有特殊字符都将被编码。您应该只对查询参数值本身进行编码。

var pinterestUrl = "http://www.pinterest.com/pin/create/button/";
pinterestUrl += "?url=" + encodeURIComponent("https://www.google.co.in/");
pinterestUrl += "&media=" + encodeURIComponent("http://www.google.co.ma/images/srpr/logo1w.png");
pinterestUrl += "&description=" + encodeURIComponent("Text Description");   
var pinterest = window.open(pinterestUrl, '_blank');

这是编写相同内容的另一种方法,可能更容易阅读。

function buildUrl(baseUrl, queryParams) {
    return Object.keys(queryParams).reduce(function(url, key) {
        return key + '=' + encodeURIComponent(queryParams[key]);
    }, baseUrl + '?');
}

function shareToPinterest() {
    var queryParams = {
        url: "https://www.google.co.in/",
        media: "http://www.google.co.ma/images/srpr/logo1w.png",
        description: "Text Description"
    };
    var pinterestUrl = buildUrl("http://www.pinterest.com/pin/create/button/", queryParams);
    var pinterest = window.open(pinterestUrl, '_blank');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多