AJAX POST和PHP [英] AJAX POST and PHP

查看:109
本文介绍了AJAX POST和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它发送POST请求到PHP的一些值的AJAX脚本。当我试图找回在PHP中的值,我不能得到任何东西。

在AJAX脚本

  xmlhttp.open(POST,handle_data.php,真正的);
xmlhttp.setRequestHeader(内容型,text / plain的);
VAR cmdStr =CMD1 = Commanda和放大器; CMD2 =命令2;
xmlhttp.send(cmdStr);
警报(xmlhttp.responseText);
 

PHP脚本是

 < PHP
  回声$ _ POST ['CMD1'];
?>
 

输出只是一个简单的空警告框。有没有在code任何错误?

解决方案

  xmlhttp.onreadystatechange =功能()
{
    如果(this.readyState == 4和&安培; this.status == 200)
    {
        如果(this.responseText!= NULL)
        {
            警报(this.responseText);
        }
    };
}
 

您需要等待要接收的数据,使用的onreadystatechange 委托回调。

http://www.w3.org/TR/XMLHtt$p$pquest /

I have a AJAX script which sends a POST request to PHP with some values. When I try to retrieve the values in PHP, am not able to get anything.

The AJAX script is

xmlhttp.open("POST","handle_data.php",true);
xmlhttp.setRequestHeader("Content-type","text/plain");
var cmdStr="cmd1=Commanda&cmd2=Command2";
xmlhttp.send(cmdStr);
alert(xmlhttp.responseText); 

The PHP script is

<?php
  echo $_POST['cmd1'];
?>

The output is just a plain empty alert box. Is there any mistake in the code?

解决方案

xmlhttp.onreadystatechange = function()
{
    if(this.readyState == 4 && this.status == 200)
    {
        if(this.responseText != null)
        {
            alert(this.responseText);
        }
    };
}

You need to wait for the data to be received, use the onreadystatechange to delegate a callback.

http://www.w3.org/TR/XMLHttpRequest/

这篇关于AJAX POST和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆