Javascript Can';在php中访问ajax post请求
我到处找了,但找不到这个问题的答案 我正在编写一个小ajax脚本,但无法获得POST请求的正确值。 这是迄今为止的代码:Javascript Can';在php中访问ajax post请求,javascript,php,jquery,arrays,ajax,Javascript,Php,Jquery,Arrays,Ajax,我到处找了,但找不到这个问题的答案 我正在编写一个小ajax脚本,但无法获得POST请求的正确值。 这是迄今为止的代码: <textarea id="message" name="message" style="width:100%;"></textarea> <input value="SEND" style="border-radius: 5px 5px 5px 5px;" type = 'button' onclick = 'ajaxFunction()'
<textarea id="message" name="message" style="width:100%;"></textarea>
<input value="SEND" style="border-radius: 5px 5px 5px 5px;" type = 'button' onclick = 'ajaxFunction()'/>
<script type="text/javascript"> <!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('chbox');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
var message = document.getElementById('message').value;
var queryString = message ;
ajaxRequest.open("POST", 'chatdata.php', true);
//ajaxRequest.send(null);
ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajaxRequest.send('queryString');
}
</script>
<?php
$message1 = $_REQUEST['message'];
echo $message;
?>
以下是您在jQuery中的操作方法—简单得多:
$('#mybutt')。单击(函数(){
var txt=$('#message').val();
$.ajax({
键入:“post”,
url:'my_ajax_processor_file.php',
数据:“ta=”+txt,
成功:功能(d){
如果(d.长度)警报(d);
}
});
}); //结束mybutt。单击
问题在于查询字符串。您应该放置ajaxRequest.send(queryString)
而不是ajaxRequest.send('queryString')代码>。不要使用查询字符串,只使用变量的名称。它应该有用 我修复了一些bug和代码启动工作:
一,
三,
您需要设置参数名。请参阅.Maybevar queryString=“queryString=”+消息代码>?请记住,post数据是一系列类似这样的key=value
对:key1=value1&key2=value2&key3=value3&……
最好定义一个使用XMLHttpRequest对象的事件处理程序,而不是定义一个直接实例化XMLHttpRequest对象的事件处理程序,2006年是互联网的第三个生日,第二个生日是同年1月(Firebug)。@AnthonyRutledge我猜你没有读过这个问题。OP评论说他更愿意在jQuery中这样做,但不知道怎么做。不过,很高兴看到你的好意评论。@gibberish抱歉,它也没有这样说。“我会使用jQuery…”他根本没有这样的变量。是的,var queryString=message中有on代码>在代码中确实,很抱歉根本没看到。不是我的,再次抱歉。(但如果能让你高兴的话,我会投票赞成:)让我们一起去吧。现在好了。在编写ajaxRequest.send('queryString')
之前,而不是现在编写的内容。Javascript将“queryString”解释为字符串而不是变量。现在您将ajaxRequest.send(“message=“+queryString”)代码>,此处queryString是变量而不是字符串
<p id="chbox"></p> <!-- ajaxDisplay need this -->
ajaxRequest.send("message="+queryString); //queryString is variable so without quotes
var_dump($message1); //there was message without 1