Turn from: Click to open link
Now the JS framework encapsulates the AJAX request very simply, such as the following:
[JavaScript] View plain copy
- $.ajax ({
- type:
- url: "some.php",
- data: { name: "John", Location: "Boston"  }  
- }). Done (function ( msg ) {
- alert ( "data saved: " + msg );
-     });
Above is a piece of Ajax request code based on the jquery framework, using the POST request method
But in some scenarios we still have to use get mode, and we need to spell out the request parameters, such as window.open, the a tag's href, and so on. Code such as this concatenation of strings is prone to errors, such as stitching all properties and values of an object into a URL's request parameter. We may need this:
[JavaScript] View plain copy
- var params = ';
- params + = ' key1= ' +obj.key1;
- params + = ' key2= ' +obj.key2;
- params + = ' key3= ' +obj.key3;
- params + = ' key4= ' +obj.key4;
- ...
Needless to say too much, painful ah, like hard labor, a little can not understand the programmer on the tall feeling. Maybe we can.
[JavaScript] View plain copy
- var params = UrlEncode (obj);
In that case, how does this happen? Take a look at a buddy on the Internet implementation of jquery-based approach
[JavaScript] View plain copy
- var parseparam=function (param, key) {
- var paramstr="";
- if (param instanceof string| | param instanceof number| | param instanceof Boolean) {
- paramstr+="&" +key+ "=" +encodeuricomponent (param);
- }else{
- $.each (param,function (i) {
- var k=key==null?i:key+ (param instanceof Array?" ["+i+"] ":". " +i);
- paramstr+=' & ' +parseparam (this, k);
- });
- }
- return Paramstr.substr (1);
- };
[JavaScript] View plain copy
- Test
- var obj={name:' Tom ',' class ': {className:' Class1 '},classmates:[{name:' Lily '}]};
- ParseParam (obj);
- Output: "Name=tom&class.classname=class1&classmates[0].name=lily"
- ParseParam (obj,' Stu ');
- Output: "Stu.name=tom&stu.class.classname=class1&stu.classmates[0].name=lily"
Reference from Liu Jiahua http://www.oschina.net/code/snippet_139242_7584
Since this code itself is not much, in order to enhance its versatility, I used native JavaScript to rewrite the method, as follows:
[JavaScript] View plain copy
- /**
- * Param the object that will be converted to the URL parameter string
- * Key URL parameter string prefix
- * Encode True/false is URL encoded, default is True
- *
- * Return URL parameter string
- */
- var UrlEncode = function (param, key, encode) {
- if (param==null) return ';
- var paramstr = ';
- var t = typeof (param);
- if (t = = ' string ' | | t = = ' number ' | | t = = ' Boolean ') {
- Paramstr + = ' & ' + key + ' = ' + ((encode==null| | Encode)? encodeURIComponent (param): param);
- } Else {
- For (var i in param) {
- var k = key = = null? I:key + (param instanceof Array?) ' [' + i + '] ': ' . ' + i);
- Paramstr + = UrlEncode (Param[i], k, encode);
- }
- }
- return paramstr;
- };
- var obj={name:' Tom ',' class ': {className:' Class1 '},classmates:[{name:' Lily '}]};
- Console.log (UrlEncode (obj));
- Output: &name=tom&class.classname=class1&classmates[0].name=lily
- Console.log (UrlEncode (obj,' Stu '));
- Output: &stu.name=tom&stu.class.classname=class1&stu.classmates[0].name=lily
JS object to URL parameter (native JS and jquery two ways)