" JSON.stringifiy"返回URLEncoded数据而不是JSON

时间:2016-06-11 15:05:50

标签: jquery html json

我正在编写一个ASP.NET Web Forms应用程序,在其中一个页面中,我有一个用于注册的模式表单,它使用JQuery .dialog()来显示它。由于ASP.NET中的限制,页面只能包含一个form元素(母版页当然包含一个元素),因此我创建的模式表单使用DIV标记。因此,外包装DIV命名为" register"。这是DIV代码:

        <div id="register" title="New Member Registration">
        <div class="modalCaption">
            <span class="red">Fields in red</span>
            <span>are required.  Your information is not shared with anyone and is fully encrypted in our
            database for your peace of mind.</span>
        </div>
        <br />
        <div class="row">
            <div class="row_label_red">Desired User Name :</div>
            <div class="row_field"><input type="text" name="userName" size="32" MaxLength="32" /></div>
        </div>
        <div class="row">
            <div class="row_label_red">Your Email :</div>
            <div class="row_field"><input type="text" name="email" size="32" MaxLength="64" /></div>
        </div>
        <div class="row">
            <div class="row_label_red">Password (6 char. min) :</div>
            <div class="row_field"><input type="password" name="pwd" size="32" MaxLength="32" /></div>
        </div>
        <div class="row">
            <div class="row_label_red">Confirm Password :</div>
            <div class="row_field"><input type="password" name="conPwd" size="32" MaxLength="32" /></div>
        </div>
        <div class="row">
            <div class="row_label">Your Last Name :</div>
            <div class="row_field"><input type="text" name="lastName" size="32" MaxLength="64" /></div>
        </div>
        <div class="row">
            <div class="row_label">Your First Name :</div>
            <div class="row_field"><input type="text" name="firstName" size="32" MaxLength="64" /></div>
        </div>
        <div class="row">
            <div class="row_label">ZIP/Postal Code :</div>
            <div class="row_field"><input type="text" name="zip" size="10" MaxLength="10" /></div>
        </div>
        <div class="row">
            <div class="row_label">How Did You Hear About Us?</div>
            <div class="row_field">
                <select name="about">
                    <option Value="NONE" Selected>-- Please Choose One --</option>
                    <option value="News">News Article</option>
                    <option value="Google">Google Search</option>
                    <option value="Bing">Bing Search</option>
                    <option value="OtherSearch">Other Search Engine</option>
                    <option value="Friend">Friend's Referral</option>
                    <option value="Miscellaneous">Other</option>
                </select>
            </div>
        </div>
        <div class="row">
            <div id="spStatus"></div>
        </div>
    </div>

要捕获表单字段数据(因为我必须使用DIV而不是form),我尝试使用JSON.stringify()将数据格式化为JSON名称/值对。为了测试这个,我使用以下内容:

var formData = JSON.stringify($.param($('#register').find(':input')));

var data = '{ "register": "' + formData + '"}"';
alert(data);

当我测试这个时,我将其作为stringify()调用的输出:

Output of web page

正如您所看到的,stringify()电话给我的是URL编码数据,这不是我需要或真正期望的。任何人都可以在这里找到我失踪的帮助吗?

1 个答案:

答案 0 :(得分:2)

不要使用$.param,它会将所有输入序列化为URL编码的字符串。循环输入元素并将它们放入对象中,然后将其转换为JSON。

var formData = {};
$('#register').find(':input').each(function() {
    formData[this.name] = this.value;
});
var json = JSON.stringify(formData);