Php $\发送后的请求变量为空
我正在为wp做一个ajax函数。但我总是得到0的回答。我看到了文件admin-ajax.php的代码,并看到了以下内容:Php $\发送后的请求变量为空,php,javascript,xmlhttprequest,Php,Javascript,Xmlhttprequest,我正在为wp做一个ajax函数。但我总是得到0的回答。我看到了文件admin-ajax.php的代码,并看到了以下内容: if ( empty( $_REQUEST['action'] ) ) die( '0' ); 这是我的js函数ajax function fnc(){ var ajax=new XMLHttpRequest(); ajax.open("POST", "<?php echo get_site_url(); ?
if ( empty( $_REQUEST['action'] ) )
die( '0' );
这是我的js函数ajax
function fnc(){
var ajax=new XMLHttpRequest();
ajax.open("POST", "<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php");
ajax.onreadystatechange= function(){
if (ajax.readyState === 4) {
if (ajax.status === 200) {
alert(ajax.responseType);
alert(ajax.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
ajax.send("action=some_function");
}
函数fnc(){
var ajax=new-XMLHttpRequest();
open(“POST”,“/wp admin/admin ajax.php”);
ajax.onreadystatechange=function(){
if(ajax.readyState==4){
如果(ajax.status==200){
警报(ajax.responseType);
警报(ajax.responseText);
}否则{
警报(“请求有问题”);
}
}
}
send(“action=some_函数”);
}
$.ajax({
类型:'POST',
url:“/wp admin/admin ajax.php”,
数据:“”,//要发布的内容
成功:功能(数据){
警报(数据);
});
}
});
}
尝试此操作为了将
发送
字符串用作表单数据,您可能需要添加以下标题:
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
否则,PHP不会将原始POST数据转换为
$\u POST
/$\u REQUEST
变量。您是否通过GET或POST将var操作传递给PHP文件?显示您的请求标题提交请求时firebug或chrome工具会说什么?参数是什么?为什么假设jQuery?这是处理ajax@cbuckley:为什么不采用jQuery?@Sorpigal,因为OP使用的是本机XMLHttpRequest对象;可能是有原因的,一个像无知这样的原因。如果您没有使用jQuery,那么您可能应该使用它;推荐它是合理的。
If you want to use javascript and XMLHttpRequest this is the full way to do that :)
function ajax_post(){
// Create our XMLHttpRequest object
var ajax=new XMLHttpRequest();
// Create data to send to our PHP file
var url = "xyz.php";
var fn = document.getElementById("a").value;
var ln = document.getElementById("b").value;
var variable = fn+" hello "+ln;
hr.open("POST", url, true);
// Set content type header for sending url encoded variables
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Get the onreadystatechange event for the XMLHttpRequest
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
var return_data = ajax.responseText;
alert(ajax.return_data);
// Send the data to PHP now... and wait for response to update the status div
ajax.send(variable); // Actually execute the request
}
}
}
If you want to use javascript and XMLHttpRequest this is the full way to do that :)
function ajax_post(){
// Create our XMLHttpRequest object
var ajax=new XMLHttpRequest();
// Create data to send to our PHP file
var url = "xyz.php";
var fn = document.getElementById("a").value;
var ln = document.getElementById("b").value;
var variable = fn+" hello "+ln;
hr.open("POST", url, true);
// Set content type header for sending url encoded variables
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Get the onreadystatechange event for the XMLHttpRequest
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
var return_data = ajax.responseText;
alert(ajax.return_data);
// Send the data to PHP now... and wait for response to update the status div
ajax.send(variable); // Actually execute the request
}
}
}