selecting params value to use with $.ajax()

Hello all,

I’m trying to write a jquery function where I am posting some data from a form. I am wondering is there a way to select the particular param that I want to use when I submit the data to $.ajax(). I don’t believe it makes sense to create a hidden field just to use with $.ajax() but I can’t seem to figure out how to get to the params key/value using jquery.

The code that I am attempting to use is:

$.ajax({

url: “/app/controller/action”,

type: “POST”,

data: {code : ‘param[:value]’ or ??}

});

Can this be done at all? if so, what would be a good approach?

Thanks,

Lets say you have a form with an ID new_form

var form = $('#new_form');

  $.ajax({

    url: form.attr("action"),

    type: "POST",    

    data: form.serialize(),

    cache: false,

    beforeSend: function(){

       //something here

    },

    complete: function(){

       // something here

    },

    success: function (result) {
        //here comes whatever you want  
    }      

});

so instead of form.serialize() you have to send the variable you want

I think in the success function, or before send you should do an alert of what is sending, then you will choose what you want to show after the success

That’s all, hope it helps

Javier Q

> Hello all,

> I'm trying to write a jquery function where I am posting some data from a
> form. I am wondering is there a way to select the particular param that I
> want to use when I submit the data to $.ajax(). I don't believe it makes
> sense to create a hidden field just to use with $.ajax() but I can't seem
> to figure out how to get to the params key/value using jquery.

> The code that I am attempting to use is:
> $.ajax({
> url: "/app/controller/action",
> type: "POST",
> data: {code : 'param[:value]' or ??}
> });

> Can this be done at all? if so, what would be a good approach?

> Thanks,

> -

Lets say you have a form with an ID new_form

var form = $('#new_form');
$.ajax({
url: form.attr("action"),
type: "POST",
data: form.serialize(),
cache: false,
beforeSend: function(){
//something here
},
complete: function(){
// something here
},
success: function (result) {

    //here comes whatever you want

\}

});

so instead of form.serialize() you have to send the variable you want
I think in the success function, or before send you should do an alert of
what is sending, then you will choose what you want to show after the
success

Thanks for the quick response. That does give me a little more clarity
but I guess my question was how do I define my variable with a value
that is in params? When I do alert($("#params")) I get a response of
object Object. When I do alert($(form.serialize())), I seem to get
most of the params values except for the one that I want. It also
doesn't look like its in an array so I can't chose the element that I
want. Granted I am new to jquery and don't fully understand
serialization and a great many other things. I believe the first alert
is telling me that I can get to the entire params object using an id
of params. But how do I get to the value within it to be able to
specify my data variable in my $.ajax() statement? Sorry If I am not
making much sense...

First of all, does #params is html? or you just put it there?

maybe if you just put something like

console.log($(form.serialize()));

and see what you’re getting. (in chrome hit f12 and go to console, in firefox use firebug)

params is a hash of all the data you are sending, its handled by rails not as an html object

Javier Q.

> Thanks for the quick response. That does give me a little more clarity
> but I guess my question was how do I define my variable with a value
> that is in params? When I do alert($("#params")) I get a response of
> object Object. When I do alert($(form.serialize())), I seem to get
> most of the params values except for the one that I want. It also
> doesn't look like its in an array so I can't chose the element that I
> want. Granted I am new to jquery and don't fully understand
> serialization and a great many other things. I believe the first alert
> is telling me that I can get to the entire params object using an id
> of params. But how do I get to the value within it to be able to
> specify my data variable in my $.ajax() statement? Sorry If I am not
> making much sense...

First of all, does #params is html? or you just put it there?

This is something I just stuck into the alert. I did not define any
html tags with an id of params.

maybe if you just put something like

console.log($(form.serialize()));

When I refresh after adding this line to my .js file I get the values
that are in the form that is displyed.
example:
utf8=%E2%9C%93&_method=put&authenticity_token=nOiekdY47fbFAHWD1rIG5rA
%2FAjBC9HD8qU2Z%2BAvh9
...
What I don't see is the values that were passed in the URL which is
what I am looking to capture.

What about security with this kind of implementation, are they secure ? What should you do in order to create safer applications ?

What about security with this kind of implementation, are they secure ?
What should you do in order to create safer applications ?

Im not sure I understand your question. I am not attempting to go
around the current security I have in place. What am I missing?