dty63504 2012-06-23 20:32
浏览 27
已采纳

基本的AJAX请求Javascript / PHP不能正常工作

I'm attempting a basic AJAX call where I send the address 'www.google.com' to urlpost.php but the current code doesn't seem to be able to detect a response (nothing happens). I tried to look for the error but I don't know where I went wrong.

Thank you all in advance!

Disclaimer: This is an variation of an example from Robin Nixon's 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']);
}

?>
  • 写回答

2条回答 默认 最新

  • doumindang2416 2012-06-23 21:33
    关注

    request.setRequestHeader("Content-length", params.length)

    params.length is undefined. You need to replace this by the length of the data you're sending

    request.send("url=www.google.com") in your case that's 18 characters.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题