douji0588 2012-05-02 08:12
浏览 81

在javascript中从php脚本获取数据

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.

  • 写回答

5条回答 默认 最新

  • duanjian9148 2012-05-02 08:16
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料