# How can I build params via javascript?

**URL:** <https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804>\
**Category:** Rails\
**Created:** [August 2, 2008, 10:03am UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804 "2008-08-02T10:03:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 10:03am UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/1 "2008-08-02T10:03:50Z")

</div>

I want use javascript to send a javascript object back to the rails.How  
can I do it.

If I write like below:  
var material={code:“000”,name:“test”};  
params={“material”:material}

then I the params look like this:  
{"\_dc"=\>“1217663839339”}

I can get nothing at the serverside.

Now I write the code like below:  
var material={code:“000”,name:“test”};  
params={“material”:Object.toJSON(material)}

and at server side,I must use “JSON.parse”  
require ‘json’  
def create  
material=Material.new(JSON.parse(params[:material]))  
end

then I can create the material object.If I write like below:  
material=Material.new(params[:material])  
I got nothing!

My question is : How can I create the params use javascript then I can  
get it direct at the serverside rather than use the “JSON.parse”

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 11:53am UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/2 "2008-08-02T11:53:49Z")

</div>

On 2 Aug 2008, at 09:03, Michael G. wrote:

> {"\_dc"=\>“1217663839339”}
> 
> I can get nothing at the serverside.
> 
> Now I write the code like below:  
> var material={code:“000”,name:“test”};  
> params={“material”:Object.toJSON(material)}

What are you then doing with the params (on the client side), ie how  
are you submitting your request?

Fred

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/3 "2008-08-02T12:28:20Z")

</div>

Frederick C. wrote:

> On 2 Aug 2008, at 09:03, Michael G. wrote:
> 
> > {"\_dc"=\>“1217663839339”}
> > 
> > I can get nothing at the serverside.
> > 
> > Now I write the code like below:  
> > var material={code:“000”,name:“test”};  
> > params={“material”:Object.toJSON(material)}
> 
> What are you then doing with the params (on the client side), ie how  
> are you submitting your request?
> 
> Fred

Hi,Fred.Thanks for your reply.  
I used the extjs on the client side,it’s a javascript framework.  
Here is a sample form:

var form=new Ext.FormPanel({  
labelWidth: 100,  
title:“material”,  
frame:true,  
border:false,  
width: 820,  
items:[  
{fieldLabel: ‘code’,id:“curcode”,name:“material[code]”},  
{fieldLabel: ‘name’,id:“curname”,name:‘material[name]’}  
],  
buttons: [  
{text:‘save’,handler:function(){  
form.getForm().submit({  
method:‘POST’,  
waitTitle:‘Connecting’,  
waitMsg:‘Sending data…’,  
url:’/material/create’,  
success:function(){Ext.Msg.alert(“sucess”,“sucess”);},  
failure:function(form, action){  
if(action.failureType == ‘server’){  
obj =  
Ext.util.JSON.decodeaction.response.responseText);  
Ext.Msg.alert(‘failure’, obj.errors);  
}else{  
Ext.Msg.alert(‘Warning!’, 'Authentication server is unreachable  
: ’ + action.response.responseText + “abcd”);}  
itemform.getForm().reset();  
} });  
}}  
]  
})

if I submit it,the params is what I want:  
{“material”=\>{“code”=\>“000”,“name”=\>“test”}}

Now I write a new application.

var queryform=new Ext.FormPanel({  
labelWidth: 100,  
title:“material”,  
frame:true,  
border:false,  
width: 820,  
items:[  
{fieldLabel: ‘code’,id:“curcode”,name:“material[code]”},  
{fieldLabel: ‘name’,id:“curname”,name:‘material[name]’}  
]  
})

var form=new Ext.FormPanel({  
labelWidth: 100,  
title:“material”,  
frame:true,  
border:false,  
width: 820,  
items:[{layout:‘form’,xtype:‘panel’,autoHeight:true}],  
buttons: [  
{text:‘save’,handler:function(){  
var material={  
code:Ext.getCmp(“code”)?Ext.getCmp(“code”).getValue():"",  
name:Ext.getCmp(“name”)?Ext.getCmp(“name”).getValue():""  
};  
var mbsstore = new Ext.data.Store(  
{  
proxy: new Ext.data.HttpProxy({url: ‘/material/queryresualt/’}),  
baseParams:{“material”:Object.toJSON(material)},  
reader: new Ext.data.JsonReader({root:‘items’},[ ‘id’,  
‘name\_native’]),  
remoteSort: true  
});  
mbsstore.load();  
}}]})

now the params is: {“material”=\>"{“code”:“000”,“name”:“test”}"}  
if I rewrite the code like this: baseParams:{“material”:material)},the  
params is: {"\_dc"=\>“1217663839339”}

My question is,I want send a javascript object(like the material) back  
to the serverside,how can I build the params like  
{“material”=\>{“code”=\>“000”,“name”=\>“test”}}

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 12:38pm UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/4 "2008-08-02T12:38:51Z")

</div>

On 2 Aug 2008, at 11:28, Michael G. wrote:

> > > params={“material”:Object.toJSON(material)}

snip

> My question is,I want send a javascript object(like the material) back  
> to the serverside,how can I build the params like  
> {“material”=\>{“code”=\>“000”,“name”=\>“test”}}

I know nothing about extjs. You need to convince it to submit data  
that looks like

material[code]=000&material[name]=test

Fred

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/5 "2008-08-02T13:25:41Z")

</div>

On 2 Aug 2008, at 12:07, Michael G. wrote:

> > > to the serverside,how can I build the params like  
> > > spectial format.But,there is some question:  
> > > I know nothing about extjs, so i’d be hard pushed to give you a sample
> 
> what is [material[code]=000&material[name]=test],a string ?  
> And where is the key?

I mean that the body of the post should be  
material[code]=000&material[name]=test

or if it’s a get request, then the url should look like

some\_controller/some\_action?material[code]=000&material[name]=test

Fred

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/6 "2008-08-02T13:32:52Z")

</div>

Frederick C. wrote:

> On 2 Aug 2008, at 12:07, Michael G. wrote:
> 
> > > > to the serverside,how can I build the params like  
> > > > spectial format.But,there is some question:  
> > > > I know nothing about extjs, so i’d be hard pushed to give you a sample
> > 
> > what is [material[code]=000&material[name]=test],a string ?  
> > And where is the key?
> 
> I mean that the body of the post should be  
> material[code]=000&material[name]=test
> 
> or if it’s a get request, then the url should look like
> 
> some\_controller/some\_action?material[code]=000&material[name]=test
> 
> Fred

Thank you,Fred.I got it.I have created the params successfully.  
I created the params like below  
params:{“material[code]”:“000”,“material[name]”:“test”}

Thand you very much!

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [August 2, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/how-can-i-build-params-via-javascript/144804/7 "2008-08-02T13:07:44Z")

</div>

Frederick C. wrote:

> On 2 Aug 2008, at 11:28, Michael G. wrote:
> 
> > > > params={“material”:Object.toJSON(material)}
> 
> snip
> 
> > My question is,I want send a javascript object(like the material) back  
> > to the serverside,how can I build the params like  
> > {“material”=\>{“code”=\>“000”,“name”=\>“test”}}
> 
> I know nothing about extjs. You need to convince it to submit data  
> that looks like
> 
> material[code]=000&material[name]=test
> 
> Fred

Hi,Fred.Can you give me a sample?I can translate the javascript to the  
spectial format.But,there is some question:

what is [material[code]=000&material[name]=test],a string ?  
And where is the key?
