Get data from php script in javascript

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.

Answers


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);
        }
    });
}

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....!


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


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 ..


Need Your Help

ASP.Net MVC Master Page architecture advice and dynamic CSS / JavaScript references?

asp.net-mvc-3 architecture master-pages

I am moving into the ASP.Net MVC realm and need some thoughts / advice on architecture. In my current ASP.Net web forms application I am using a nested master page approach. I have a Main site master

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.