【问题标题】:how to pass data from javascript to json如何将数据从javascript传递到json
【发布时间】:2014-12-04 07:05:36
【问题描述】:

我正在开发一个用于创建一种社交链接储物柜的静态网站的插件。

我正在尝试将数据从 javascript 传递到我的 json,但我遇到了一些问题。代码如下:sn-p。

这有点长,但我正在尝试将 curr_page 变量传递到 json 字符串中,以便正确共享每个页面。如果我手动输入静态 url,按钮会起作用,如果我尝试传入 page 或 curr_page 变量,它就不起作用。

如何将 curr_page 变量传递到该 json 字符串中?

<script type="text/javascript">
    $(document).ready(function () {
        var curr_page = window.location.href;
        page = String(curr_page);
        console.log(curr_page);
        console.log(page);
        $("#sharer").sociallocker({
            text: {
                header: "Grab the extra resources",
                message: "Please support us, click like button below to unlock the content."
            },

            locker: {
                close: false,
                timer: 0
            },

            buttons: {   
                order: ["facebook-like", "twitter-tweet", "twitter-follow", "google-plus"]
            },

            facebook: {
                like: {
                    title: "Like",
                    url: "https://www.facebook.com/[]"
                }
            },

            twitter: {
                tweet: {
                    title: "Tweet",
                    text: "[]",
                    url: page 
                },
                follow: {
                    title: "Follow us",
                    url: "https://twitter.com/[username]"
                }
            },

            google: {
                plus: {
                    title: "google-share",
                    url: page
                }
            }

        });
    });
</script>

【问题讨论】:

  • 您能否将输出提供给 console.log() 函数?
  • 控制台将记录正确的页面前:domain.com/correct-url 但试图将其作为字符串并将其作为“页面”传递到 json 中它不起作用,也没有尝试直接访问“ json 中的 curr_page' 似乎也可以工作。

标签: javascript jquery json social-networking


【解决方案1】:

我希望我理解你的意思...

如果你想为 JSON 设置/添加一个值,你可以这样做

myJSON.myKey = "myValue";
myJSON.myKey.mySubKey = "myValue";

就是这样……

【讨论】:

    【解决方案2】:

    我不确定你想要什么,但是如果你只想通过当前页面你可以直接输入url: window.location.href

    这里要清楚,这是一个 javascript 对象,而不是 JSON 字符串。您可以将变量或从函数返回到url:

    我不明白为什么不适合你。

    检查我的小提琴here

    【讨论】:

      【解决方案3】:

      通过传递当前页面 url,您将页面重定向到自身,通过在页面变量中提供指向自身的链接。

         url: page
      

      这里,url 仅指当前页面。并且点击这些社交按钮只会重新加载同一页面。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-01
        • 2017-11-10
        • 2012-11-09
        • 1970-01-01
        • 2017-01-14
        • 2014-02-14
        • 1970-01-01
        相关资源
        最近更新 更多