2012-08-04 70 views
1

我无法使代码的这部分工作,基本上我想调用这个函数,它发送一个变量到一个php页面。我测试了变量是否存在,并且测试了我的php页面正在接受信息,但是我无法使这个Ajax工作。Javascript-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; 
?> 
+1

我可能会得到热量,但我比如jQuery的ajax函数。很好很简单。 http://api.jquery.com/jQuery.ajax/,你不需要担心浏览器兼容性问题。 – AlexMA 2012-08-04 16:41:33

回答

3

好吧,首先,你需要改变你的请求,从POST 来得到这样

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

像AlexMA我建议你应该使用jQuery的Ajax,因为它是一个很容易使用,如果你习惯于jQuery编程 – 2012-08-04 16:47:14

+0

所以我改变了你告诉我nd有一个更新,与现在的回调函数警报我看到变量myname但是我仍然失踪看到变量时,我去http://localhost/main.php,仍然有未定义的索引... – Etienne 2012-08-04 17:12:43

+0

,除非你想它'POST' - 那么你将需要发送参数分开 – starbeamrainbowlabs 2012-08-04 17:14:36

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']; 
} 
+2

你的** GET **使用** POST ** – 2012-08-04 17:04:35

+0

@Rajan我也试过你告诉我的,现在我没有得到未定义的函数,但是我仍然可以在刷新main.php文件时看到变量。 – Etienne 2012-08-04 17:23:30

+0

我只是一个代码片段而不是整个功能。你只需要用相应的线代替它。 – 2012-08-04 17:42:46