param: function( a ) {   
        var s = [ ];   
  
        function add( key, value ){   
            s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);   
        };   
  
        // If an array was passed in, assume that it is an array   
        // of form elements   
        if ( jQuery.isArray(a) || a.jquery )   
            // Serialize the form elements   
            jQuery.each( a, function(){   
                add( this.name, this.value );   
            });   
  
        // Otherwise, assume that it's an object of key/value pairs   
        else   
            // Serialize the key/values   
            for ( var j in a )   
                // If the value is an array then the key names need to be repeated   
                if ( jQuery.isArray(a[j]) )   
                    jQuery.each( a[j], function(){   
                        add( j, this );   
                    });   
                else   
                    add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );   
  
        // Return the resulting serialization   
        return s.join("&").replace(/%20/g, "+");   
    }  为什么要替换下呢,为什么要替换成加号呢?return s.join("&").replace(/%20/g, "+");如果用户在输入框中输入了空格的字符串;我用JQuery的序列化方法得到URL串然后提交的方式,就会造成后台数据的不正确,不知道有没有好的办法?

解决方案 »

  1.   

    return s.join("&").replace(/%20/g, "+");不用+替换,改用空行不行?
      

  2.   

    json2.js
    在json object和json string之间转换
      

  3.   

    把空格换成 + 号,不是因为JSON的原因
    而是因为要把JSON的内容上传到服务器,上传的内容要经过一些编码转换
    比如 = & 符号等这些特殊符号是一定要经过转换的,而空格在上传的时候,就要转换成 + 才会被服务器正确识别。而 + 号本身,自然就要转成另一种形式了
      

  4.   

    意思是说这个转换是为了JSON的?如果不用JSON可以不转换吧?
      

  5.   

    如果你不想把整个JSON的内容上传到服务器,就不用去转换空格转换空格的目的,是上传给服务器识别的