Saurabh
Saurabh

Reputation: 159

Can't we send an integer along jQuery .post method?

I have a form and I want to send a integer from one of the textbox back to the server. I was using $.post() and wrote code as follows:

$.post("/getvalue",$("#info1").val, function(data,status){
            alert("reached server");
            $("#info2").val(data);
        });

This turned out to be not working or more clearly its sending a zero value to server. I also tried following version of above code:

$.post("/getvalue",$("#info1").serialize(), function(data,status){
            alert("reached server");
            $("#info2").val(data);
        });

$.post("/getvalue",'$("#info1").serialize()', function(data,status){
            alert("reached server");
            $("#info2").val(data);
        });

Then I also tried to store text box value into a variable and send it as follow:

var value= $("#info1").val();
$.post("/getvalue",'value', function(data,status){
            alert("reached server");
            $("#info2").val(data);
        });

Also I tried:

var value= $("#info1").val();
$.post("/getvalue",value, function(data,status){
            alert("reached server");
            $("#info2").val(data);
        });

But none of all these code works. So now my Query is that Can we not pass an integer or variable containing integer using $.post(). As the documentation says we can do so. How is it possible to do so... Am I wrong somewhere??? Believe me there are no coding errors (like parenthesis and semicolons etc..) in my full code.

I know about the $.ajax() method of doing so, but I just wanted to know the scope for doing it using simple $.post().

Please someone care to lend a hand.

Thanx

Upvotes: 1

Views: 3175

Answers (2)

user5296864
user5296864

Reputation:

Use parseInt() to convert into interger

Upvotes: 1

You need to use parseInt() in order to be able to convert it into an interger.

You can read something about it here

Also take a look at your $.post("/getvalue",$("#info1").val Your missing the () at the end of .val It shall look like .val()

Upvotes: 1

Related Questions