php - 基本 AJAX 请求 Javascript/PHP 不工作

标签 php javascript ajax

我正在尝试一个基本的 AJAX 调用,我将地址“www.google.com”发送到 urlpost.php 但当前代码似乎无法检测到响应(什么都没发生)。我试图查找错误,但我不知道哪里出错了。

提前谢谢大家!

免责声明:这是 Robin Nixon 的 Learning PHP, MySQL & Javascript 示例的变体

index.html

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Ajax Example</title>
    </head>
    <body>

    <h1 align='center'>Loading a web page into a DIV</h1>
    <div id='info' align='center'>This Sentence will be replaced </div>
    <script>
        request = new ajaxRequest()
        request.open("POST", "urlpost.php", true) //also tried 'test/urlpost.php' (test is the folder of the file)
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        request.setRequestHeader("Content-length", params.length)
        request.setRequestHeader("Connection", "close")                

        request.send("url=www.google.com")

        request.onreadystatechange = ajaxCallback

        function ajaxCallback()
        {
            alert("Reponse received") //just letting me know I got a reponse
            if(this.readyState==4) //ready
            {
                if (this.status==200) //no idea
                    {
                        if (this.responseText != null)
                            {
                                document.getElementById('info').innerHTML = this.responseText
                            }
                        else alert("AJAX ERROR: NO DATA RECEIVED")
                    }
                    else alert("Ajax error: "+this.statusText);
            }
        }

        function ajaxRequest()
        {
            try //non IE browser
            {
                var request = new XMLHttpRequest();
            }
            catch(e1)
            {
                try // IE6+?
                {
                    request = new ActiveXObject("Msxml2.XMLHTTP")
                }
                catch(e2)
                {
                    try // IE5?
                    {
                        request = new ActiveXObject("Microsoft.XMLHTTP")                   
                    }
                    catch(e3) //No Ajax support
                    {
                        request = false
                    }
                }
            }   
            return request
        }

    </script>

urlpost.php

<?php
if (isset($_POST['url'])){
    echo file_get_contents("http://".$_POST['url']);
}

?>

最佳答案

用jQuery重写,简单多了。

$(document).ready(function(){

        $.ajax({
            type: "post",
            url: "urlpost.php",
            data: "url=www.google.com",

            success: function(msg){
                $('#replace').html(msg);

            }
        });
    return false;

});​

关于php - 基本 AJAX 请求 Javascript/PHP 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11172760/

相关文章:

php - webm转换的编解码器参数不正确

javascript - 如何使用 JavaScript 验证输入字段

javascript - 如何使用数组和字符串参数进行 Ajax 调用

javascript - 如何在 getCurrentPosition 回调后执行 ajax 异步调用。

javascript - 显示 “No matches found” 或隐藏 DIV 结果(AJAX 和 MySQL)

php - 错误使用 CodeIgniter form_validation() 函数引发 fatal error

php - drupal中的日期时间插入问题

php - Mysql php/pdo 语句执行时间在刷新时显着不同

javascript - 如何编写 keyup 函数

javascript - Backbone.Model => 未捕获的类型错误 : Object [Object object] has not method 'apply'