0

I have a problem when I try to send some data with $.post(jquery ajax function) - it's not working. My code is here:

$.post(
    $(this).attr("action"),
    {     
        task: "add",
        $(this).serialize()
    },
    function(data) {
        if (data.length > 0 ) {
            alert("Success");
        }
    }
);

I am guessing the problem is with the data being sent, actually I have one complete AJAX page with lots of switch case statements to perform so for that I need to specify the task variable every time I send and AJAX request.

If there are better solutions on how to solve this issue feel free to share your thoughts. Thank you.

4
  • $(this) referring to the form??
    – Ankit
    Commented Jan 31, 2013 at 13:18
  • yes it refers to the form that i m submitting
    – Chakra
    Commented Jan 31, 2013 at 13:20
  • can you brief your code more....!! Commented Jan 31, 2013 at 13:21
  • Actually this is the first time i am using that function $(this).serialize() function when sending the form, but the problem is the action for for this ajax request has lots of switch cases, so usually i used to use variable called "task" and specify the the operation, but now when i used the serialize() i dont know how to give, which operation to be performed in the switch statement.Hope your got what i was trying to say.
    – Chakra
    Commented Jan 31, 2013 at 13:31

3 Answers 3

1

It should have been like below I think.

$.post(
    $(this).attr("action"),
    {task:"add",'data': $(this).serialize()},
    function(data){
        if(data.length > 0 ){
            alert("Success");
        }
    }
);
7
  • it works but how would i receive that values from variable "data" in php action page. If u could share some idea on that Ankit.
    – Chakra
    Commented Jan 31, 2013 at 13:26
  • how do you receive task in php? data will also be received same. Just replace 'task' to 'data' in your statement. i.e $_REQUEST['data']
    – Ankit
    Commented Jan 31, 2013 at 13:29
  • This is probably not what you are looking for. Even if it would work, the server would only receive two post parameters, "task" and "data", the latter just being a long string of combined form field-value pairs. Commented Jan 31, 2013 at 13:29
  • @PeterHerdenborg is right. Look here for the solution stackoverflow.com/questions/6164691/…
    – Ankit
    Commented Jan 31, 2013 at 13:32
  • yes Mr.Peter you are right, if u send it in variable data then all the values will get trued into very long string. Is there anything more effective to that.
    – Chakra
    Commented Jan 31, 2013 at 13:33
1

The problem is with this part:

{     
    task: "add",
    $(this).serialize()
}

That's going to throw a syntax error because it's not a valid object literal. Calling $(this).serialize() returns a string, which is the query string for the request. What you could do is this instead:

$.post($(this).attr('action'), $(this).serialize() + '&task=add', function(data) {...});
1
  • Oh thank you Mr. Anthony . it worked so here we are actually trying to concatenate another string and then send it. Thank u so much.
    – Chakra
    Commented Jan 31, 2013 at 13:40
0

The problem is probably that the part

{task:"add",$(this).serialize()}

is invalid as a label is required after the comma.

{task: "add", data: $(this).serialize()}

would at least result in a valid object, but not one that makes sense to post to the server.

I think the easiest solution would be something like

$.post($(this).attr("action"),
$(this).serialize() + '&task=add',
function(data){
 if(data.length > 0 ){
   alert("Success");
}
});
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.