简体   繁体   中英

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. 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.

Below is the HTML Page with the Javascript:

<!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
$email = $_GET['email'];
echo "Hello World!";
?>

Why does this return nothing to the JS, even though the PHP page echoes the text correctly?

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 .

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

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

See: http://enable-cors.org/

i dont like using the XMLHTTP request. instead i use jQuery's method $.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

these are the most common of the components that are used in 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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