Ajax通过url发送参数

Vin*_*nce 2 parameters ajax get send

我是ajax的新手,并认为我将成为一个有趣的实验进入我的项目.我创建了自己的灯箱类型功能,以便在我正在创建的网站上发送消息.当用户点击"发送消息"时,那就是灯箱出现的时候,在顶部我试图让它说"向用户发送消息",其中用户是他们发送消息的用户的名字.我的lightbox html元素实际上是在一个单独的网页上,这就是我使用ajax的原因.这是我到目前为止,似乎无法弄清楚问题是什么:

user.php页面

<div id = "pageMiddle"> // This div is where all the main content is.
  <button onclick = "showMessageBox(UsersName)">Send Message</button>
</div>
Run Code Online (Sandbox Code Playgroud)

注意:用户名正确地传递到javascript函数中,我已经检查了那么多.

main.js页面

function showMessageBox(user){
  alert(user); // where i checked if username passes correctly
  var ajaxObject = null;
  if (window.XMLHttpRequest){
    ajaxObject = new XMLHttpRequest();
  }else if (window.ActiveXObject){
    ajaxObject = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (ajaxObject != null){
    ajaxObject.open("GET", "message_form.php", true);
    ajaxObject.send("u="+user);
  }else{
    alert("You do not have a compatible browser");
  }
  ajaxObject.onreadystatechange = function(){
    if (ajaxObject.readyState == 4 && ajaxObject.status == 200){
      document.getElementById("ajaxResult").innerHTML = ajaxObject.responseText;
      // use jquery to fade out the background and fade in the message box
      $("#pageMiddle").fadeTo("slow", 0.2);
      $("#messageFormFG").fadeIn("slow");
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

message_form.php页面

<div id = "messageFormFG">
  <div class = "messageFormTitle">Sending message to <?php echo $_GET['u']; ?></div>
</div>
Run Code Online (Sandbox Code Playgroud)

注意:直接通过URL访问此页面时,为其提供u和值的参数,它会正确显示

Bri*_*wis 6

使用jQuery.ajax(); http://api.jquery.com/jQuery.ajax/

$.ajax({
  type: "GET",
  url: "message_form.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});
Run Code Online (Sandbox Code Playgroud)