nickelxk5
nickelxk5

Reputation: 1

jQuery $.post() data not being read correctly

I'm running into a weird bug when trying to communicate between pages using jQuery and AJAX. I simply can't get the $.post() function to work!

$(document).ready(function() {
    $('#form').submit(function() {  
        $.post('test.php', {'name': 'joey', 'age': 12}, function(data){
                     alert(data);
        });

        return false;
    });
});     

test.php:

<php?
    print_r($_POST);
?>

what happens when I do this is I get an alert with [object XMLDocument], but if I add 'text', as the final parameter in post, it alerts with the full php code of the file.

My problem is when i call:

$.post('test.php', {'name': 'joey', 'age': 12}, function(data){
                 $('#content').html(data);
       });

this brings up an error on firebug. 'content' is a div. It works when I do .html('test'), but not if I do .html(data).

Any idea what I'm doing wrong?

EDIT: here is a dropbox folder with my entire project (like 3 files), if anyone could spend 3-4 minutes looking through it, it would help me a ton! I've been stuck here for a couple days

https://www.dropbox.com/sh/ns73titud46kea7/aElaYx728G

Upvotes: 0

Views: 255

Answers (3)

Shaun Poore
Shaun Poore

Reputation: 642

I'm a little late on the reply but could it be that you have invalid json?

{'name': 'joey', 'age': 12}

should be

'{"name": "joey", "age": "12"}'

Upvotes: 0

Dutchie432
Dutchie432

Reputation: 29160

It shouldn't matter, but try setting data's type to 'html'

$.post('test.php', {'name': 'joey', 'age': 12}, function(data){
    $('#content').html(data);
}, 'html');

Or try using .load()

$('#content').load('test.php', {'name': 'joey', 'age': 12});

you might also want to try putting 12 in quotes, just to rules things out...

$('#content').load('test.php', {'name': 'joey', 'age': '12'});

Upvotes: 1

matt3141
matt3141

Reputation: 4423

<php?
    print_r($_POST);
?>

should read

<?php
    print_r($_POST);
?>

I assume the php is being interpreted as an XML node, but obviously it isn't valid html

Upvotes: 2

Related Questions