By constructing Ajax parameters to realize the conversion of _ajax JSON to the correlation

Source: Internet
Author: User

Ajax submits the server data and collates the conversion methods.

Html:

<form id= "FM" name= "FM" action= "" >
<input name= "UserName" type= "text" value= "UserName1"/>
</ Form>

1. The form element turns querystring

 
 

2. Strings, JSON converting to each other

var obj = Jquery.parsejson (' {' name ': ' John '} ');

You can use the Jquery-json plug-in to implement transformations, directly referencing the example

var thing = {plugin: ' Jquery-json ', version:2.3};
var encoded = $.tojson (thing);
' {' plugin ': ' Jquery-json ', ' Version ': 2.3} '
var name = $.evaljson (encoded). Plugin;
"Jquery-json"
var version = $.evaljson (encoded). version;

3. Form, Element Name,value array

var arr = $ ("#fm, #UserId"). Serializearray ();
/*[ 
{name: ' UserName ', Value: ' UserName ' 1 '}, 
{name: ' UserId ', Value: ' UserId '}

4. Form elements turn to JSON

$.fn.serializeobject = function ()
{
var o = {};
var a = This.serializearray ();
$.each (A, function () {
if (O[this.name]!== undefined) {
if (!o[this.name].push) {
O[this.name] = [o[ THIS.name]];
}
O[this.name].push (This.value | | '');
} else {
O[this.name] = This.value | | '';
}
});
return o;
};
var obj = $ (' form '). SerializeObject ();
/*obj:object
UserId: "UserId1"
UserName: "UserName1"

5. Json2form

$.getjson (' Url_to_file ', function (data) {for
(var i in data) {
$ (' input[name= ' ' +i+ '] '). Val (data[i));
}

Google has found a more powerful plugin in the process http://code.google.com/p/jquery-load-json/

data = {
"Name": "Emkay Entertainments",
"Address": "Nobel House, Regent Centre",
"Contacts": "Phone"
} 
$ (' Div#data '). Loadjson (data);
<div id= "Data" >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.