1

我正在使用带有以下代码的 FB 注册插件:

<html xmlns:fb="http://ogp.me/ns/fb#">

    <script type="text/javascript">
    window.fbAsyncInit = function() { FB.init({appId: 'xxx', status: true, cookie: true, xfbml: true}); };
    (function() {
        var e = document.createElement('script');
        e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    })();
</script>

这是我想要的字段列表:

                            <?php /* There seems to be a character limit for fields causing "invalid client_id" on log in/out */?>
                        <fb:registration redirect-uri="https://xxx.secure.xxx.com/register/fb_submit.php"
                            fields='[
                            {"name":"name","view":"prefilled"},
                            {"name":"email"},
                            {"name":"password"},
                            {"name":"s_question","description":"Secret Question","type":"select","options":{"2":"In which city were you born?","4":"What is your favorite book?","3":"What is your favorite pets name?","6":"What is your favorite vacation spot?","1":"What is your mothers maiden name?"}},
                            {"name":"s_answer","type":"text","description":"Secret Answer"},
                            {"name":"first_name"},
                            {"name":"last_name"},
                            {"name":"address_co","description":"Care of (Optional)","type":"text","no_submit":true},
                            {"name":"address","description":"Mailing Address","type":"text"},
                            {"name":"address2","description":"Apartment/Unit","type":"text","no_submit":true},
                            {"name":"street_address","description":"Street Address (if using a P.O. box above)","type":"text","no_submit":true},
                            {"name":"location","view":"prefilled"},
                            {"name":"city","view":"not_prefilled","description":"City","type":"text"},
                            {"name":"state","view":"not_prefilled", "description":"State","type":"select","options":{"AA":"AA - Armed Forces Americas","AE":"AE - Armed Forces","AK":"AK - Alaska","AL":"AL - Alabama","AP":"AP - Armed Forces Pacific","AR":"AR - Arkansas","AS":"AS - American Samoa","AZ":"AZ - Arizona","CA":"CA - California","CO":"CO - Colorado","CT":"CT - Connecticut","DC":"DC - District of Columbia","DE":"DE - Delaware","FL":"FL - Florida","FM":"FM - Federated States of Micronesia","GA":"GA - Georgia","GU":"GU - Guam","HI":"HI - Hawaii","IA":"IA - Iowa","ID":"ID - Idaho","IL":"IL - Illinois","IN":"IN - Indiana","KS":"KS - Kansas","KY":"KY - Kentucky","LA":"LA - Louisiana","MA":"MA - Massachusetts","MD":"MD - Maryland","ME":"ME - Maine","MH":"MH - Marshall Islands","MI":"MI - Michigan","MN":"MN - Minnesota","MO":"MO - Missouri","MP":"MP - Northern Mariana Islands","MS":"MS - Mississippi","MT":"MT - Montana","NC":"NC - North Carolina","ND":"ND - North Dakota","NE":"NE - Nebraska","NH":"NH - New Hampshire","NJ":"NJ - New Jersey","NM":"NM - New Mexico","NV":"NV - Nevada","NY":"NY - New York","OH":"OH - Ohio","OK":"OK - Oklahoma","OR":"OR - Oregon","PA":"PA - Pennsylvania","PR":"PR - Puerto Rico","PW":"PW - Palau","RI":"RI - Rhode Island","SC":"SC - South Carolina","SD":"SD - South Dakota","TN":"TN - Tennessee","TX":"TX - Texas","UT":"UT - Utah","VA":"VA - Virginia","VI":"VI - Virgin Islands","VT":"VT - Vermont","WA":"WA - Washington","WI":"WI - Wisconsin","WV":"WV - West Virginia","WY":"WY - Wyoming"}},
                            {"name":"zip","description":"5-digit Zip Code","type":"text"},
                            {"name":"phone","description":"Phone (800-555-1212)","type":"text"},
                            {"name":"referred_by","description":"Referred by-Email or site nickname (Optional)","type":"text","no_submit":true},
                            {"name":"cert_code","description":"Gift Certificate Code (Optional)","type":"text","no_submit":true},
                            {"name":"sel_hear_about","description":"How Did You Hear About Us?","type":"select","options":{"1":"Friend","2":"Newspaper","3":"Magazine","4":"Internet Search","5":"Internet Ad","6":"Website","8":"site2","9":"site3","10":"Other","11":"TV","12":"Radio"}},
                            {"name":"hear_about","description":"newspaper/blog/website/friend name? (Optional) ","type":"text","no_submit":true},
                            {"name":"birthday"},
                            {"name":"hours","description":"Hours Spent Reading per Week (Optional)","type":"select","options":{"0":"0","1-5":"1-5","6-10":"6-10","11-20":"11-20","21-30":"21-30","31-40":"31-40","41+":"41+"},"no_submit":true}
                            ]' onvalidate="validate">
                        </fb:registration>

这是我的 validate() 函数:

        function validate(form)
    {
        errors = {};
        var dt = new Date(), expiryTime = dt.setTime( dt.getTime() + 1000*5 );
        var expires = dt.toGMTString();

        if (form.first_name == "")
        {
            errors.first_name = "Please choose a First Name";
        }

        if (form.s_question == "-1")
        {
            errors.s_question = "Please choose a Secret Question";
        }
        if (form.s_answer == "")
        {
            errors.s_answer = "Please type an answer to your Secret Question";
        }
        if (form.address_co !== "")
        {
            set_cookie( 'address_co', form.address_co, expires, '/register', 'secure.xxx.com', true );
        }
        if (form.address == "")
        {
            errors.address = "Please type your mailing address";
        }
        if (form.address2 !== "")
        {
            set_cookie( 'address2', form.address2, expires, '/register', 'secure.xxx.com', true );
        }
        if (form.street_address !== "")
        {
            set_cookie( 'street_address', form.street_address, expires, '/register', 'secure.xxx.com', true );
        }
        if (form.zip == "")
        {
            errors.zip = "Please type a 5-digit Zip Code"
        }
        else if (form.zip.length != 5)
        {
            errors.zip = "Zip Code must be 5 digits";
        }
        else if (isNaN(form.zip))
        {
            errors.zip = "Zip Code must be a number";
        }


        if (form.phone == "")
        {
            errors.phone = "Please type your phone number";
        }
        if (form.referred_by !== "")
        {
            set_cookie( 'referred_by', form.referred_by, expires, '/register', 'secure.xxx.com', true );
        }
        if (form.cert_code !== "")
        {
            set_cookie( 'cert_code', form.cert_code, expires, '/register', 'secure.xxx.com', true );
        }
        if (!form.sel_hear_about)
        {
            errors.sel_hear_about = "Please choose how you heard about us";
        }
        if (form.hear_about !== "")
        {
            set_cookie( 'hear_about', form.hear_about, expires, '/register', 'secure.xxx.com', true );
        }
        if (form.hours !== "-1")
        {
            set_cookie( 'hours', form.hours, expires, '/register', 'secure.xxx.com', true );
        }

        return errors;
    }

如果用户在访问此注册表单时已经登录到 FB,则填充 FB 字段并且该表单有效。但是,如果用户没有登录 FB 并在表单上选择 FB 提示“登录以使用您的个人资料信息预先填写下面的表单。”,然后向 FB 提供登录凭据,该表单就会消失并被相当通用的替换来自 FB 的“client_id”错误

如果用户随后刷新页面,则他/她已登录 FB,并填充了正确的 FB 字段。

我已经验证了 JSON 字段列表。在测试过程中,我发现如果我将字段列表缩减为:

                                fields='[
                            {"name":"name","view":"prefilled"},
                            {"name":"email"},
                            {"name":"password"},
                            {"name":"s_question","description":"Secret Question","type":"select","options":{"2":"In which city were you born?","4":"What is your favorite book?","3":"What is your favorite pets name?","6":"What is your favorite vacation spot?","1":"What is your mothers maiden name?"}},
                            {"name":"s_answer","type":"text","description":"Secret Answer"},
                            {"name":"first_name"},
                            {"name":"last_name"},                                                               ]' onvalidate="validate">

然后我没有得到错误。如果我重新添加最长的字段(上面的状态字段),则会返回 client_id 错误。除了添加状态,我可以添加其他较短字段的组合并得到相同的错误。这让我觉得我达到了 JSON 字段列表的某种最大大小限制。可能不是发送到 FB 的实际 JSON,而是在登录后 FB 发回的相应隐藏字段中。

如果我无法解决这个问题,我计划只获取基本的 FB 字段并将其余字段移至另一个页面。当然,由于表格和我的支持注册,我更喜欢将它留在一块。将 FB 数据加载到表单中(刷新时)后,代码所有工作。

感谢您对这个冗长的描述的耐心等待。

4

1 回答 1

0

我对此没有任何解决办法,我只能提供一个可能的(不,我实际上认为最有可能的)解释:

与所有其他插件一样,该<fb:registration>标签的作用基本上是在您的页面中创建一个 iframe,它将所有必要的参数作为 GET 参数传递到查询字符串中。

因此,您为注册表单定义的许多字段意味着 iframe 的 URL 中的查询字符串很长。

问题是,浏览器限制了 URL 的可能长度 - 请参阅https://stackoverflow.com/a/417184/1427878http://www.boutell.com/newfaq/misc/urllength.html

Web 服务器也这样做,在他们说,“呃,这对我来说通过 GET 处理有点多,很抱歉,但抱歉......”</p>

因此,虽然您的注册表单可能适用于用户已登录时定义的许多字段,因为 iframe URL 的长度保持在您的浏览器或 Facebook 的服务器强制执行的限制之下——如果用户未登录并单击登录按钮,它们通过 Facebook Auth 流程重定向,并且在成功登录后它们应该“发送回”的 URL(在这种情况下是带有所有 GET 参数的注册表单的地址)作为 GET 参数传输到Auth 对话 URL 以及其他参数。(并且在将一个 URL 作为 GET 参数传输到另一个 URL 中时应用的必要 URL 编码会更糟……)

这会导致一个非常长的URL,在浏览器或 Facebook 服务器强制执行的限制下,无法再传输/处理该 URL。(由于您收到 Facebook 的错误消息,因此很可能 Facebook 的服务器拒绝在这种情况下处理它。)

因此,您可以尝试使用 Facebook 打开错误报告——他们设置最大 GET URL 长度的限制可以解决此问题;但如果他们愿意这样做,可能是另一个问题。(因为允许更长的请求 URL 可能还有其他含义。)

And even if they did, you might still hit road blocks with the browsers your users are using – if the current limit in Internet Explorer is still 2,048 characters for the path part of an URL, that might be the next part where the registration form with to many fields will break on the frontend …</p>

于 2012-09-29T17:39:17.257 回答