Javascript jquery 序列化和 encodeURIComponent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4277758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:41:52  来源:igfitidea点击:

jquery serialize and encodeURIComponent

javascriptjqueryajax

提问by Gary

I need to URI encode a form input, that is then serialized with a bunch of a hidden inputs and sent to a PHP file.. is it possible to somehow combine encodeURIComponent into this line?:

我需要对表单输入进行 URI 编码,然后用一堆隐藏输入序列化并发送到 PHP 文件..是否有可能以某种方式将 encodeURIComponent 合并到这一行中?:

var landingCreate = $(this).serialize();

UPDATE:

更新:

Doing this for example:

这样做例如:

var landingCreate = $(this).serialize()+"&enc="+encodeURIComponent($('input[name=\'longform\']').val());

and entering the url:

并输入网址:

http://www.smashingmagazine.com/2008/10/13/pricing-tables-showcase-examples-and-best-practices/

into the text box, returns the URL unchanged.. shouldnt it be converting all the dashes and slashes etc to hex codes?

进入文本框,返回 URL 不变.. 不应该将所有破折号和斜线等转换为十六进制代码吗?

UPDATE

更新

Here is the full code.

这是完整的代码。

<form id="createTokenLanding">
    <input type="text" name="longform" />
    <input type="hidden" name="domain" value="<?php echo rawurlencode($_SERVER['HTTP_HOST']); ?>" />
    <input type="hidden" name="useragent" value="<?php echo rawurlencode($_SERVER['HTTP_USER_AGENT']); ?>" />
    <input type="hidden" name="ip" value="<?php echo rawurlencode($_SERVER['REMOTE_ADDR']); ?>" />
    <input type="hidden" name="cookieuser" value="<?php echo rawurlencode($_COOKIE['littlr_user']); ?>" />
    <input type="submit" name="submit" value="Shorten" />
</form>

<div id="result">
123
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $.ajaxSetup ({ cache: false });
        $('#createTokenLanding').submit(function() {
            var landingCreate = $('#createTokenLanding').serialize();
            $.ajax({
                url:    'action-create.php',
                data:   landingCreate,
                success: function(responseText){
                        $('#result').html(responseText);
                }
            });
            return false;
        });
    });
</script>

回答by andres descalzo

if you use jQuery.ajax, you can see the documentation for the option "traditional." Also if you use jQuery 1.4 has the "traditional" in "jQuery.param." what they do is use the function "encodeURIComponent" for the key and value:

如果您使用 jQuery.ajax,您可以查看“传统”选项的文档。此外,如果您使用 jQuery 1.4,则在“jQuery.param”中有“传统”。他们所做的是使用函数“encodeURIComponent”作为键和值:

param = encodeURIComponent (key) + "=" + encodeURIComponent (value);

find traditional setting
jQuery.param

查找传统设置
jQuery.param

UPDATE

更新

you can see from this example, "serialize" works well over the fields to be sent by post or get. Can you put the ajax code to which you send data? example

您可以从这个示例中看到,“序列化”在要通过 post 或 get 发送的字段上运行良好。你能把你发送数据的ajax代码放上去吗? 例子

回答by Juan de Dios

Can you try urlencode functionand use .replace() for hex codes.

您可以尝试 urlencode 函数并使用 .replace() 来表示十六进制代码。