0

I am beginning to play around with JSON, and I keep running into trouble that neither Google nor SO have helped me with. I have a very simple PHP script:

<?php

$email = $_REQUEST['email'];

if ( strpos($email,'@') !== false ) {
    $data = array('status' => 1 , 'msg' => 'Sent') ;
    echo json_encode( $data ) ;
} 

else {
    $data = array('status' => 0 , 'msg' => 'Failed to send') ;
    echo json_encode( $data ) ; 
}


?>

I have the following ajax call:

$('.submit').click(function() {
    $('div.load').html('<img src="images/load.gif" alt="Loading..." id="loading" />'); //EDIT

    //creation of variables to send
    var name = $('#name').val();
        email = $('#email').val();
        phone = $('#phone').val();

    $.ajax({
        type: "POST",
        dataType: "jason",

        data: {
            name: name,
            email: email,
            phone: phone
        },

        url: "test.php",

        success: function( data ) {

            $('.contact').append( data )

        }
    });

    return false; 

});

If the php gets called without JS (and the form doesn't contain a proper email address), then I get the following object (which is what I want!): {"status":0,"msg":"Failed to send"}

However, if submitting with JS (ajax), the JSON object never gets received. Any ideas?

Thanks!

5
  • Note that you mistyped the ajax datatype param (should be json instead of jason) Commented Jun 4, 2012 at 22:43
  • May not be an issue here, but don't you have a typo in dataType? I don't know any jason. Commented Jun 4, 2012 at 22:43
  • Are the PHP headers set to send JSON data? Commented Jun 4, 2012 at 22:46
  • also, are you sure you want to append the JSON object to $(".contact"), don't you want to do something with the object first? Commented Jun 4, 2012 at 22:48
  • @dotty: I don't actually want to append the object. But before I actually used the object, I wanted to make sure I'm properly receiving it Commented Jun 4, 2012 at 22:49

1 Answer 1

3
dataType: "jason",

Read:

dataType: "json",

;-)

Also, you have some semi colons where there should be commas:

var name = $('#name').val(),    // These two lines should be comma-terminated to
    email = $('#email').val(),  // make this a correct var declaration
    phone = $('#phone').val();
Sign up to request clarification or add additional context in comments.

6 Comments

Haha, nice catch. Is that the only bug that you see, because having fixed that, I still don't get the json object returned :(
@Amit What do you see when you console.log(data); in the success function? Also, try adding an error handler
Interesting! If I console.log(data), I do seem to get the proper object. I wonder why it doesn't want to append it?
How do you want it to display? You can't really just append a non-DOM object to the DOM, it needs to be some form of string representation...
For example you could $('.contact').append("<div>Status: "+data.status+"; Message: "+data.msg+"</div>");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.