0

i am sending a post request using jquery $.post to another domain. every thing works fine but i am not getting the posted data in the requested page please check my code

jquery code

    var data = {mydata: 'testing'};
      $.post("http://anyurl/file.php",data,function(info){
        alert(info);
    });

and here is php code

<?php

header('Access-Control-Allow-Origin: *'); // this is to allow another domain

$data = $_POST[“mydata”]; // assigning data to variable
echo $data; // sending back to jquery

?>

it does not return the data please check anyone.

Thanks in advance

7
  • 2
    I don't know if its just for me but your variable: $_POST[“mydata”] should have proper quotations like $_POST[''mydata] Commented Nov 14, 2013 at 5:36
  • Yep, incorrect quotation marks Commented Nov 14, 2013 at 5:37
  • 1
    Curly/smart quotes; beautiful yet deadly. Commented Nov 14, 2013 at 5:40
  • 1
    @Fred-ii- like many exes :) Commented Nov 14, 2013 at 5:45
  • @dbh what are you suggesting man $_POST[''mydata] this is incorrect Commented Nov 14, 2013 at 5:50

3 Answers 3

3

Use proper quotes and use isset() to check the data set or not like,

<?php
    header('Access-Control-Allow-Origin: *'); // this is to allow another domain    
    print_r($_POST);// to check the post data
    $data = isset($_POST['mydata']) ? $_POST['mydata'] : "No Data";
    echo $data; // sending back to jquery
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Try to get the POST variable with single quotes and better to put exit after echoing it like

$data = $_POST['mydata'];
echo $data; 
exit;

And make sure that it is posting to the given URL or not through console.

1 Comment

Try to get the POST variable and put exit after echoing it like
0

OK,It walk well when I test it.In order to reappear the error you've got,I'm trying to set the file.php into a BOM-UTF8 file and it got error in the end.So,please check your file.php if it got a BOM before header().

Another suggess,you can use firebug to test your ajax post process.The link in the console will show you the ajax process,such as what you've post and what is feedback.Just use console.log() instead of alert().

1 Comment

I like it giving the reason to down vote in the comment at least he could get some knowledge and will not suggest the same thing to someone else It would be much appreciated if all members provide reasons for the down-vote

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.