Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过AJAX将值从JavaScript发布到PHP,无需jQuery_Javascript_Php_Ajax_Post - Fatal编程技术网

通过AJAX将值从JavaScript发布到PHP,无需jQuery

通过AJAX将值从JavaScript发布到PHP,无需jQuery,javascript,php,ajax,post,Javascript,Php,Ajax,Post,我试图发送一个JavaScript变量,由PHP在服务器端处理,然后回显结果。我的ajax请求导致readyState==4和status==200,但PHP一直回显空数组/值 我很确定这是因为我的Ajax调用发送了一个空请求,但我尝试的一切都不起作用,所以我可能在这里出了问题 JS(来自index.html): PHP(来自test.PHP): 我最后一次尝试是更改request.send(valueToSend)

我试图发送一个JavaScript变量,由PHP在服务器端处理,然后回显结果。我的ajax请求导致
readyState==4
status==200
,但PHP一直回显空数组/值

我很确定这是因为我的Ajax调用发送了一个空请求,但我尝试的一切都不起作用,所以我可能在这里出了问题

JS(来自
index.html
):

PHP(来自
test.PHP
):

我最后一次尝试是更改
request.send(valueToSend).send(encodeURI('name='+valueToSend))

我做错了什么?

您的代码中几乎没有什么地方出错,例如:

  • 即使您定义了
    send\u ajax()
    函数,也没有在代码中的任何地方调用它
  • 使用
    POST
    请求,您需要随请求一起发送HTTP头
    setRequestHeader()
    ,如下所示:

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    request.send("name="+valueToSend);
    
    var valueToSend = Math.random(); 
    
    var request = false;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        request = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    function send_ajax() {
        if (request) {
            request.open('POST', 'test.php', true);
            request.onreadystatechange = function() {
                if (request.readyState == 4 && request.status == 200) {
                    console.log(request.responseText);
                }
            }
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            request.send("name="+valueToSend);
        }
    }
    
    send_ajax();
    
  • echo$_POST['name']无效,因为这里未定义
    name
    。您的
    send()
    方法调用应该如下所示:

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    request.send("name="+valueToSend);
    
    var valueToSend = Math.random(); 
    
    var request = false;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        request = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    function send_ajax() {
        if (request) {
            request.open('POST', 'test.php', true);
            request.onreadystatechange = function() {
                if (request.readyState == 4 && request.status == 200) {
                    console.log(request.responseText);
                }
            }
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            request.send("name="+valueToSend);
        }
    }
    
    send_ajax();
    
因此,完整的JavaScript代码如下所示:

request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("name="+valueToSend);
var valueToSend = Math.random(); 

var request = false;
if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    request = new ActiveXObject('Microsoft.XMLHTTP');
}

function send_ajax() {
    if (request) {
        request.open('POST', 'test.php', true);
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                console.log(request.responseText);
            }
        }
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send("name="+valueToSend);
    }
}

send_ajax();

test.php的路径正确吗?请仔细检查:是的,它是正确的。哦,我的上帝。。。我完全忘记了
setRequestHeader()
方法。。。现在工作很好。非常感谢。