0

I am sending to the server this call:

var Message = {};
Message['islands'] = '1-15-13';
Message['newMessageText'] = 'this is test message';
$.ajax({
    url: "sendnote.php",
    type: "POST",
    data: Message,
    dataType: "json",
    contentType: "charset=utf-8",
    success: function (data) {
        alert(data["result"]);
    },
    error: function (data) {
        alert(data["result"]);
    }
});

and on server (sendnote.php) I have

print_r($_POST);

just to check do I receive anything, but after cchecking response in Firebug I see that this array is empty.

What am I doing wrong in sending data?

Thanks in advance!

PS I've checked previous post on this subject, but still have problems with it.

3
  • Hmm, how did you get data["result"]? Is there more to the sendnote.php script that you are not showing? Or is it just print_r($_POST)? Commented Mar 27, 2012 at 6:27
  • When I run that code, I can see the data being POSTed. Commented Mar 27, 2012 at 6:27
  • Quentin, you are right, I see that too, but in Firebug I see that my $_POST is empty. That's the problem. That's why I did just print_r($_POST) Commented Mar 27, 2012 at 7:14

2 Answers 2

1

The problem is the contentType

Try this:

jQuery

$(document).ready(function(){
var Message = {};
    Message['islands'] = "1-15-13";
    Message['newMessageText'] = 'this is test message';

$.ajax({
        url: "sendnote.php",
        type: "POST",
        data: Message,
        dataType: "json",
        success:function(data) {
            alert("Islands: "+data.islands+", Message: "+data.newMessageText);
            },
        error:function(data) {
            alert('error');
        }  });
});

php

<?php
    echo json_encode($_POST);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

print_r($_POST) does not give a JSON response. do you even know what the actual reply of the request looks like when not using AJAX?

try echo json_encode($_POST); - this should print out a valid JSON.

or might i add that you might have forgotten PHP's <?php ?> opening and close tags

2 Comments

I think he's checking the ajax request in Firebug. But yeah, I think you are right if sendnote.php is just print_r($POST);
Gohn67, you are right. I am just checking $_POST in Firebug. I have in my code $jsonresponse = json_encode($_POST) and other code which creates JSON response. On the client side I receive JSON response that is obviously result of empty $_POST.

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.