1

I try to receive a PHP response in my JavaScript.

My PHP looks like this:

some code

    if(...) echo "1";

    else echo "2";

JavaScript:


    function GetChoice() {
        var returned="";
        $.ajax({
            async: false,
            cache: false,
            url: "http://mydomain.com/script.php", 
            type: "POST",
            dataType:"text",
            success: function(data) { 
                returned = data;
            }
        });
        return returned;
    }

    var r = GetChoice();
    alert(r);

But GetChoice() returns nothing. What's wrong?

UPD: It works if javascript and php script are on the same server. My scripts in different domains.

Amadeus
  • 19
  • 1
  • 4
  • 1
    If you use Firefox Firebug then you can check in the Net tab if you are getting any ajax response – skos May 02 '12 at 08:18
  • Post your real php code where you check $_POST params – Denis Ermolin May 02 '12 at 08:18
  • Why are you using an absolute path? Is it from the same server? – Shikiryu May 02 '12 at 08:38
  • If you use FF or Chrome you can use the development tools, then see what the response is that is being returned from your server. $.ajax should log a request in the "network" tab in developer tools (Chrome). Click on the request and then click on response to see what the server sent back. Maybe your server isn't sending anything back? – Jacques Koekemoer Apr 08 '15 at 09:20

6 Answers6

2

Try this :

temp1.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>


  function GetChoice() {
        var returned = "";
        $.ajax({
                async: false,
                cache: false,
                type: "POST",
                url: "http://localhost/temp2.php",
                data: { name: "John"}
                }).done(function( msg ) {                        
                        returned = msg;
                });
         return returned;
    }

    var r = GetChoice();
    alert(r);

</script>

temp2.php

<?php

        echo $_REQUEST["name"];        
?>

its working....!

Chintan
  • 1,204
  • 1
  • 8
  • 22
  • This only works because 'localhost' is the domain in your example, and both scripts are running on it. OP says their scripts are on two different domains. PHP script would need the additional header added before any output... 'header('Access-Control-Allow-Origin: *');' Also, temp1.php is not PHP in this example, it's HTML calling javascript resources. Jus say'n. – Epiphany Aug 09 '18 at 03:38
1

try this:

    function GetChoice() {
    var returned = "";
    $.ajax({
        async:false,
        cache:false,
        url:"http://mydomain.com/script.php",
        type:"POST",
        dataType:"text",
        success:function (data) {
            alert(data);
        }
    });
}
Kevin Wang
  • 82
  • 2
  • And how can I resolve it? I want to get receive form php script on domain1.com in javascript on site in domain2.com. – Amadeus May 02 '12 at 08:53
  • If this is cross-domain issue, then be sure your PHP scripts know this by adding 'header('Access-Control-Allow-Origin: *');' before any output in your PHP script. – Epiphany Aug 09 '18 at 03:31
1

The problem is, in your example, $.ajax returns immediately and the next statement, return result;, is executed before the function you passed as success callback was even called. Here is explanation. How do I return the response from an asynchronous call?

Luck,

Community
  • 1
  • 1
Blarz
  • 294
  • 3
  • 6
0

GetChoice() will return nothing before the callback in success runs.

The callback, which is the function you define as the success paramater will not fire until the data have been requested from the server.

This is asyncronous (the A in AJAX) so the rest of the code with continue causing the GetChoice() function to return before the callback has been run

Andrew Hall
  • 3,058
  • 21
  • 30
0

this is the script

<script type="text/javascript">
$.ajax({
async:false,
cache:false,
url:"http://path.com/to/file",
type:"POST",
dataType: "html",
data: 'data',
success: function(data){
    alert(data);
}

});

and in your PHP file write this code

<?php

function test()
{
    $str = 'This is php file';
    return $str;
}

echo test();

?>

Make sure the path to the php file is correct AND add the script in another PHP file. Basically you need 2 files. Just tested this in my editor and works ..

Skecci Dev
  • 121
  • 4
0
function GetChoice() {
    var returned="";
    $.ajax({
        url: "../script.php", 
        type: "POST",
        success: function(data) { 
            returned = data;
        }
    });
    return returned;
}

var r = GetChoice();
alert(r);