Javascript-Ajax无法发送任何数据

时间:2012-08-04 16:35:39

标签: 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;
?>

2 个答案:

答案 0 :(得分:3)

首先,您需要将您的请求从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!";
}
?>

答案 1 :(得分:0)

有两种方法可以向服务器发送ajax请求 GET或POST

<强> 1。 GET方法:

var url='http://localhost/main.php?Name='+myname; // you can add any numner of vars here
AJAX.open("GET", url, true); 
AJAX.send();

main.php中的代码

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        echo $_GET['Name'];
    }

<强> 2。 POST方法:

AJAX.open("POST","ajax_test.asp",true);
AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
AJAX.send("Name="+myname);

main.php中的代码

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    echo $_POST['Name'];
}