数据不会从ajax请求存储到$ _POST数组中

数据不会从ajax请求存储到$ _POST数组中

问题描述:

So I've already done an AJAX request using GET, and so now i wanted to try my luck using POST instead. But for some reason, when i try to send data, I get a crazy weird message in the console - NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED: 'JavaScript component does not have a method named: "available"' when calling method: [nsIInputStream::available] I literally have no idea what this means, and I know the data isnt going through because all im doing in the load.php file that I request is echo the variable its supposed to store. So its something in the javascript.

Here is my HTML for the first page that makes the request.

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<input id="input">

<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

And my Javascript:

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  var data = "id="+document.getElementById("input").value;
xmlhttp.open("POST","load.php",true);
xmlhttp.send(data);
}

And finally, the code for load.php:

$param = $_POST['id'];
if($param){
        echo "Variable was stored.";
    } else{
        echo "Not working";
    }

And everytime i run this, i get "not working" in the browser. So the php code is at least attempting to store the variable, but its not. Thankyou!

所以我已经使用GET完成了一个AJAX请求,所以现在我想用POST来试试运气 。 但由于某种原因,当我尝试发送数据时,我在控制台中收到一条疯狂奇怪的消息 - NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED:'JavaScript组件没有名为:“available”的方法,当调用方法时:[nsIInputStream :: available] I 字面上不知道这意味着什么,我知道数据不会通过,因为我请求的load.php文件中的所有我正在回应它应该存储的变量。 所以它是javascript中的东西。 p>

这是我的第一页请求的HTML。 p>

 &lt;!DOCTYPE html&gt  ; 
&lt; html&gt; 
&lt; head&gt; 
&lt; script src =“// ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js”&gt;&lt; / script&gt; \  n&lt; script type =“text / javascript”src =“test.js”&gt;&lt; / script&gt; 
&lt; / head&gt; 
&lt; body&gt; 
 
&lt; div id =“myDiv”&gt;&lt;  h2&gt;让AJAX更改此文本&lt; / h2&gt;&lt; / div&gt; 
&lt; input id =“input”&gt; 
 
&lt; button type =“button”onclick =“loadXMLDoc()”&gt;更改内容&lt;  / button&gt; 
 
&lt; / body&gt; 
&lt; / html&gt; 
  code>  pre> 
 
 

我的Javascript: p>

  function loadXMLDoc()
 {
var xmlhttp; 
if(window.XMLHttpRequest)
 {//代码用于IE7 +,Firefox,Chrome,Opera,Safari 
 xmlhttp = new XMLHttpRequest(); 
} 
else  
 {// IE6的代码,IE5 
 xmlhttp =新的ActiveXObject(“Microsoft.XMLHTTP”); 
} 
 
 
 
 
 
 
 
 
 
 
真实地交换=函数(
)
 {
 if(xmlhttp.readyState == 4&amp;  &amp; xmlhttp.status == 200)  
 {
 document.getElementById(“myDiv”)。innerHTML = xmlhttp.responseText; 
} 
} 
 var data =“id =”+ document.getElementById(“input”)。value; 
xmlhttp。  open(“POST”,“load.php”,true); 
 
xmlhttp.send(data); 
} 
  code>  pre> 
 
 

最后,加载代码 .php: p>

  $ param = $ _POST ['id']; 
if($ param){
 echo“存储了变量。”; 
} else {  
 echo“不工作”; 
} 
  code>  pre> 
 
 

每次运行此操作时,我都会在浏览器中“无法正常工作”。 所以php代码至少试图存储变量,但事实并非如此。 谢谢你! p> div>

Your forgot to add xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'). With this line we are basically saying that the data send is in the format of a form submission

function loadXMLDoc()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
        }
          var data = "id="+document.getElementById("input").value;

        xmlhttp.open("POST","load.php",true);
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(data);
    }