php - Javascript-Ajax 无法发送任何数据

标签 php javascript ajax

我在使这部分代码工作时遇到问题,基本上我想调用这个将变量发送到 PHP 页面的函数。我测试了该变量是否存在,并且还测试了我的 php 页面是否按应有的方式接受信息,但是我无法使这个 Ajax 工作正常。

function ajaxRequest(myname) {
var AJAX = null; // Initialize the AJAX variable.
if (window.XMLHttpRequest) 
{ // Does this browser have an XMLHttpRequest object?
    AJAX=new XMLHttpRequest(); // Yes -- initialize it.
} else 
{ // No, try to initialize it IE style
    AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
} // End setup Ajax.

if (AJAX==null) 
{ // If we couldn't initialize Ajax...
    alert("Your browser doesn't support AJAX."); // Sorry msg.
return false // Return false, couldn't set up ajax
}
AJAX.onreadystatechange = function() 
{ // When the browser has the request info..
    if (AJAX.readyState==4 || AJAX.readyState=="complete") 
    { // see if the complete flag is set.
        callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
    } // End Ajax readystate check.
}

alert("Alert1");
var url='http://localhost/main.php?Name=myname';    
AJAX.open("POST", url, true); // Open the url this object was set-up with.
alert("Alert2");
AJAX.send(); // Send the request.

这是我的 php 部分,它应该接受变量

<?php
$var=$_GET['Name'];

echo $var;
?>

最佳答案

好的,首先您需要将您的请求从 POST 更改为 GET 喜欢

AJAX.open("GET", url, true); // Open the url this object was set-up with.

你还需要更新这一行 来自

var url='http://localhost/main.php?Name=myname'; 

var url='http://localhost/main.php?Name='+myname; 

我的完整脚本是:

<script type="text/javascript">
    function ajaxRequest(myname) {
        var AJAX = null; // Initialize the AJAX variable.
        if (window.XMLHttpRequest) 
        { // Does this browser have an XMLHttpRequest object?
            AJAX=new XMLHttpRequest(); // Yes -- initialize it.
        } else { // No, try to initialize it IE style
            AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
        } // End setup Ajax.

        if (AJAX==null) 
        { // If we couldn't initialize Ajax...
            alert("Your browser doesn't support AJAX."); // Sorry msg.
            return false // Return false, couldn't set up ajax
        }

        AJAX.onreadystatechange = function() 
        { // When the browser has the request info..
            if (AJAX.readyState==4 || AJAX.readyState=="complete") 
            { // see if the complete flag is set.
                callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
            } // End Ajax readystate check.
        }

        alert("Alert1");
        var url='http://localhost/main.php?Name='+myname;    
        AJAX.open("GET", url, true); // Open the url this object was set-up with.
        alert("Alert2");
        AJAX.send(); // Send the request.
    }
</script>

你可能还缺少回调函数,所以添加它,使其看起来像这样

function callback(x, y) {
    alert(x);
}

然后调用你的 AJAX 函数

ajaxRequest("ashley");

这是您需要的 main.php 代码(尽管这不是您应该使用 AJAX 的目的

<?php 
session_start();
if(isset($_GET["Name"])) {
   $_SESSION["Name"] = $_GET["Name"];
}
if(isset($_SESSION["Name"])) {
   echo $_SESSION["Name"];
} else {
   echo "The AJAX has not been run!";
}
?>

关于php - Javascript-Ajax 无法发送任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11810236/

相关文章:

php - Python 开发 - 将结果存储在 for 循环中

javascript - 如何在php中检索ajax发送的数据?

javascript - 使用 XMLHttpRequest 下载二进制数据,无需 overrideMimeType

jquery - 轨道 3 : Can't get form to be processed as javascript instead of HTML

javascript - Angular UI 多个弹出窗口,提交后以编程方式关闭单个弹出窗口

php - Pretty Photo 通过 ajax 框架加载图片

php - php 会增加页面加载时间吗?

java - 接口(interface):php 与 java

php - 使用 file() 函数的 Foreach 连续循环。

javascript - 匹配 JS 中除反向引用之外的所有字符