简体   繁体   English

即使PHP页面回显某些内容,对PHP页面的Ajax调用也始终不返回任何内容

[英]Ajax call to a PHP page always returns nothing even though the PHP page echoes something

I am calling a very simple PHP page with some equally simple AJAX, but the call always returns nothing, even though the PHP is fine. 我正在用一个同样简单的AJAX调用一个非常简单的PHP页面,但是即使PHP很好,该调用也始终不返回任何内容。 That is, you can go to the URL of the PHP page and see that it echoes "Hello World" but when it is called with JS, it returns nothing. 也就是说,您可以转到PHP页面的URL,并看到它回显“ Hello World”,但是用JS调用时,它什么也不返回。

Below is the HTML Page with the Javascript: 以下是带有Javascript的HTML页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title of the document</title>
</head>

<body>
The content of the document......<br />

Enter your email: <input id="email" type="text" />
<input type="button" onclick="setXMLHttpRequest()" value="Go!" />

<script type='text/javascript'/>

        var http;

        function setXMLHttpRequest()
        {
            if(window.XMLHttpRequest)
                http = new XMLHttpRequest();
            else if(window.ActiveXObject)
                http = new ActiveXObject("Microsoft.XMLHTTP");

                url = "http://www.convolutedconstruct.com /Ajax/checkemail.php?email=" + 
                                   document.getElementById('email').value;
                http.onreadystatechange = display;
                http.open("GET", url, true);
                http.send(null);

        }

        function display()
        {
            if (http.readyState == 4)
            {   
                infostr = http.responseText;
                alert("From the PHP: " + infostr);
            }
        }
</script></body></html>

Here is the content of the PHP page Click here for the live PHP page 这是PHP页面的内容单击此处,进入实时PHP页面

<?php
$email = $_GET['email'];
echo "Hello World!";
?>

Why does this return nothing to the JS, even though the PHP page echoes the text correctly? 为什么即使PHP页面正确回显了文本,这也没有返回JS?

As has been suggested above, AJAX request will only work usually when both the caller and called are on same domain, You have to ensure that your html code, which contains the javascript, resides on same domain http://www.convolutedconstruct.com . 如上所述,AJAX请求通常仅在调用方和被调用方都在同一域上时才起作用。您必须确保包含javascript的html代码位于同一域http://www.convolutedconstruct.com上

If that is not the case you can use CORS to allow your ajax to receive input from your php page by sending this header in your php output 如果不是这种情况,您可以使用CORS通过在php输出中发送此标头来允许ajax从php页面接收输入

<?php
header("Access-Control-Allow-Origin: *");
//rest of your code
?>

See: http://enable-cors.org/ 请参阅: http//enable-cors.org/

i dont like using the XMLHTTP request. 我不喜欢使用XMLHTTP请求。 instead i use jQuery's method $.ajax({}); 相反,我使用jQuery的方法$.ajax({}); method. 方法。 it always works for me! 它总是对我有用!

$.ajax({
    type: "POST", // or 'GET'
    url: "your-url.php", // url that you are passing the data to
    data: {
        dataName: 'data to pass' // string, variable, object, array, etc
    },
    success: function(output) { // output is what the url is 'echoing' back to the jQuery
        // do something when the ajax method is complete.
    }
});

dont forget to import the jQuery source code - http://code.jquery.com/jquery-1.7.2.min.js 不要忘记导入jQuery源代码 -http://code.jquery.com/jquery-1.7.2.min.js

these are the most common of the components that are used in ajax. 这些是ajax中最常用的组件。

I'll be glad to help you out some more if you would like it. 如果您愿意,我很乐意为您提供更多帮助。

If you want to know more just check the documentation on it: http://api.jquery.com/jQuery.ajax/ 如果您想了解更多信息,请查看文档: http : //api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM