tags:

views:

338

answers:

3

I am using jquery-1.3.2 in an AJAX web application. I use the jQuery ajax $.post() method to submit requests to the server.

On the server I am using php to build an array and then json_encode the answer. Then on the client I use the callback function of the AJAX post method to process the response.

All works well until I use the $.post() method to send variables to the server. If I send variables to the server, the response I get back is [object Object] and therefore I am unable to parse it. I have a work around at the moment that when posting variables I request a HTML response and then I parse that.

So the code involved taken from my site is:

The Jax call:

$.post("inc/sendfeedback.php", {NAME: name,TYPE: type,EMAIL: email,COMMENT: comment}, function(data) {PostData(data);}, "json");

So the PostData code looks like this:

function ProcessData(data)
{
    //alert(data);
    var jo = eval("(" + data + ")");
    if(jo.result == "true")
    {
     if(jo.data != "" && jo.element != "")
     {
      $(jo.element).html(jo.data);
     }
    }

    SMessage(jo.error);
}

If I uncomment the above code the alert with have in it [object Object].

if I remove the Post variables from the call it works fine.

The server code look like this:

$arr = array ("result" => $result,"data" => $data,"error" => $error,"element" => $element);
echo(json_encode($arr));

Is this a bug with the jQuery library, I tried it with the 1.2 version however its was still present there? I also search the jQuery site and can not find anyone having this issue.

So I assume I am missing something. But what?

+5  A: 
$.ajax({
      url: "script.php",
      global: false,
      type: "POST",
      data: {NAME: name,TYPE: type,EMAIL: email,COMMENT: comment},
      dataType: "json",
      contentType: "application/json",
      success: function(data){
         alert(data.result);
      }
   }

No need to eval, jQuery evals/parses it before calling the success callback.

eval = pure evil

http://docs.jquery.com/Ajax/jQuery.ajax#options

Chad Grant
eval isn't pure evil. It's a little too slow to be pure.
epochwolf
he didn't need that pesky session cookie anyways ;)
Chad Grant
I just implemented the code and get the exact same error.Which is: missing ] after element list ([object Object])using the method I posted this error goes away when you dont post any variables.Normally I would have thought something was wrong with the php json_encode function, but posting variables to the server has no impact on this function and therefore does not make sense.
Post the JSON output so I can try to help please, hard to do this blind
Chad Grant
Ok heres the response data from the server:{"result":"true","data":"<div class='FeedbackForm'><br\/><br\/>Thank you for your feed back. Should we need to follow up on your comments we will use the email and name supplied.<br\/><br\/>The Ping You Games Team.<\/div>","error":"Feedback Sent Successfully","element":".Menu7"}As far as I can tell theres nothing wrong with that object.Is in the jquery library I believe, because thats the exact format my other json objects come back in when I dont post any variables.The bug in the library is to do with a missing ]. Ill find the bug.
That's valid JSON and it's not an array. Just remove your eval and it should be fine. var jo = data; alert(jo.result);
Chad Grant
+1  A: 

Because you are using an associative PHP array, json_encode will return a string representation of a Javascript Object and not a Javascript Array. However, you should still be able to process it in a similar fashion to an array:

for (var key in data)
{
    var item = data[key];
}

I would strongly recommend you download Firefox+Firebug addon and use the console API for debugging/dumping what is being returned by the server.

Jordan S. Jones
Yeah I do that.Whats throwing me is if I dont post variables in the call to the server, everything works.It only breaks when I post variables as part of the original ajax post call.The bug I feel is under the hood in the object jquery is creating for the call. I haven't pulled the jquery call apart to see as I thought I would ask here first.
Jordan S. Jones
A: 

I have since registered and now can't post comments into this thread without reputation and can not see any easy method to claim this question as mine.

Deviant, your suggestion of using the $.ajax() method worked. Reason it didnt work for me the first time was I submitted the post data as a JSON object when the server code was expecting POST data.

So I fixed my javascript to call the server script correctly and everything works exactly as it should.

So the conclusion is, the $.post() method has a bug in it. I have not tracked it down but line 3633 is were the post method makes the call. I started digging however have not yet found the issue.

I qualify this by the fact the $.ajax() to the same server script and the same javascript processes the response and it all works, use the $.post method and my script fails with the return even through the return object appears to be a valid JSON object.

Thanks for the help guys. Now to go and remove all my $.post calls for $.ajax calls.

The result of all this can be seen at www.pygames.net

Cheers Shane a.k.a FrogSkin

Cool, then can you please accept the answer?
Chad Grant