Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.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
Javascript Ajax-don';得不到请求_Javascript_Php_Ajax - Fatal编程技术网

Javascript Ajax-don';得不到请求

Javascript Ajax-don';得不到请求,javascript,php,ajax,Javascript,Php,Ajax,我有一个JavaScript函数,用于启动请求。我需要这个请求的GET参数,但是试图通过PHP访问它不会返回任何结果。知道为什么吗? 我在同一个PHP文件(index.PHP)中调用JS函数 JavaScript: function aufloesung() { var request = new XMLHttpRequest(); request.open("GET", "index.php?screen=1", true); request.send(); }

我有一个JavaScript函数,用于启动请求。我需要这个请求的GET参数,但是试图通过PHP访问它不会返回任何结果。知道为什么吗? 我在同一个PHP文件(index.PHP)中调用JS函数

JavaScript:

function aufloesung() {
    var request = new XMLHttpRequest();
    request.open("GET", "index.php?screen=1", true);

    request.send();

}
PHP文件:

<script> aufloesung(); </script>

...

echo $_GET["screen"]
aufloesung();
...
echo$\u GET[“屏幕”]

但是我没有得到参数。

使用jQuery并分割index.php和ajaxphp文件很容易

index.php中包含jquery.js

<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
    aufloesung();
</script>
ajax.php:

<?PHP

    echo $_GET[ 'screen' ];

?>

您正在发出两个单独的HTTP请求

第一种方法是在浏览器的地址栏中键入URL,它不包含查询字符串参数,而是在页面中呈现

第二个是使用XMLHttpRequest对象生成的,它确实包含了查询字符串参数,但是您没有对响应执行任何操作,因此无法看到它

例如,您可以:

function aufloesung() {
    var request = new XMLHttpRequest();
    request.open("GET", "index.php?screen=1", true);
    request.addEventListener("load", function (event) {
        document.body.appendChild(
            document.createTextNode(this.responseText)
        );
    });
    request.send();
}

这个调用正确吗:code.jquery.com/jquery latest.js“>,在代码和src之间http://your 代码不是jQuery,但如果您想在页面中使用jQuery,可以使用:
$.ajax({type:“get”,url:“index.php?screen=1”,success:function(data){….})
但这要看情况而定。Screen可以是1或2。我必须如何编写函数才能返回正确的值?最好是对索引和ajax(PHP)文件进行切片。为了便于了解,我现在将编辑我的代码。我编写了:$.ajax({type:'get',url:'index.PHP?Screen=1',success:function(data){alert('hier');});我得到了mesage hier,但php没有得到“screen”的值“您在浏览器中查看的html不能被PHP修改,因此您发送的ajax请求将返回该PHP文件响应的全新副本,包括脚本。然后,您只需获取该文本并对其进行处理,例如将其附加到页面中。如果你把它分成两个php文件可能会更好。我看到了textnode,但是php没有得到“屏幕”,或者我看不到值,那么你需要调试它。更详细地描述这个问题。查看浏览器的开发人员工具。看看是否有人提出请求。看看你是否得到了预期的回应。查看控制台中是否有错误。问题是,我没有获取Screen的值,或者我看不到该值。这只是我的问题,我没有任何错误,
function aufloesung() {
    var request = new XMLHttpRequest();
    request.open("GET", "index.php?screen=1", true);
    request.addEventListener("load", function (event) {
        document.body.appendChild(
            document.createTextNode(this.responseText)
        );
    });
    request.send();
}