标题:求教:想用html5改写程序,点击按钮,获得网页数据,好像遇到了跨域问题。
只看楼主
wanguhssheng
Rank: 2
等 级:论坛游民
威 望:2
帖 子:131
专家分:24
注 册:2016-2-26
结帖率:79.17%
已结贴  问题点数:20 回复次数:17 
求教:想用html5改写程序,点击按钮,获得网页数据,好像遇到了跨域问题。
<!DOCTYPE HTML>
<html>
    <head>
        <title>Cross Origin Test</title>
    </head>
    <script>
        function getFromNet(){
            alert("here is me");
            
                var httpRequest = new XMLHttpRequest();
                var url = "http://qt.
                alert(url);
                httpRequest.onreadystatechange = handleResponse;
                httpRequest.open("GET", url);
                httpRequest.response.setHeader("Access-Control-Allow-Origin","*");
                httpRequest.response.setHeader("Access-Control-Allow-Methods","GET");
                httpRequest.response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
                httpRequest.send();
            }

            function handleResponse(){
                alert("here");
               
                alert(httpRequest.responseText);
            }

    </script>
    <body>
        <button onclick = "getFromNet()">ClickMe</button>
    </body>
</html>
搜索更多相关主题的帖子: html5 数据 alert url response 
2018-08-25 07:22
红星二锅头
Rank: 8Rank: 8
等 级:贵宾
威 望:46
帖 子:491
专家分:754
注 册:2016-8-25
得分:7 
HttpRequest = Createobject("MSXML2.XMLHTTP")
HttpRequest.Open("GET", Alltrim(Thisform.text1.Value), .T.)
HttpRequest.setRequestHeader("Access-Control-Allow-Origin","*")
HttpRequest.setRequestHeader("Access-Control-Allow-Methods","GET")
HttpRequest.setRequestHeader("Access-Control-Allow-Headers","content-type")
HttpRequest.setRequestHeader("Connection", "Close")

HttpRequest.Send( )

Do While HttpRequest.readyState != 4
    =Inkey(0.1)
Enddo

If HttpRequest.Status = 200
    Thisform.Edit1.Value = HttpRequest.ResponseText
Endif

HttpRequest = .Null.


.

团队红星二锅头,队长王猛,领队李冲,顾问杨伟,副队张挺,外联马彪,内勤韩秀,人事顾闯,财务赵魁,后勤周强
2018-08-25 10:26
wanguhssheng
Rank: 2
等 级:论坛游民
威 望:2
帖 子:131
专家分:24
注 册:2016-2-26
得分:0 
谢谢,我试一下。
2018-08-25 12:12
wanguhssheng
Rank: 2
等 级:论坛游民
威 望:2
帖 子:131
专家分:24
注 册:2016-2-26
得分:0 
<!DOCTYPE HTML>
<html>
    <head>
        <title>Cross Origin Test</title>
    </head>
    <script>
        function getFromNet(){
            alert("here is me");
            
                var httpRequest = new Msxml2.XMLHTTP();
                var url = "http://qt.
                alert(url);
                //httpRequest.onreadystatechange = handleResponse;
                httpRequest.open("GET", url, true);
                httpRequest.setRequestHeader("Access-Control-Allow-Origin","*");
                httpRequest.setRequestHeader("Access-Control-Allow-Methods","GET");
                httpRequest.setRequestHeader("Access-Control-Allow-Headers","content-type");
                httpRequest.setRequestHeader("Connection", "Close")
                httpRequest.send();
            }

            function handleResponse(){
                alert("here");
               
                alert(httpRequest.responseText);
            }

    </script>
    <body>
        <button onclick = "getFromNet()">ClickMe</button>
    </body>
</html>
改写成了这样,在 win7 + QQ浏览器中不行,在手机android 4.4.2 + UC浏览器中也不行,是不是版本问题呀?
2018-08-25 13:00
红星二锅头
Rank: 8Rank: 8
等 级:贵宾
威 望:46
帖 子:491
专家分:754
注 册:2016-8-25
得分:0 
你要纯 HTML 代码?不需要转成 VFP 的?
你走错地方了,,,

团队红星二锅头,队长王猛,领队李冲,顾问杨伟,副队张挺,外联马彪,内勤韩秀,人事顾闯,财务赵魁,后勤周强
2018-08-25 13:50
红星二锅头
Rank: 8Rank: 8
等 级:贵宾
威 望:46
帖 子:491
专家分:754
注 册:2016-8-25
得分:0 
<!DOCTYPE HTML>
 <html>
     <head>
         <title>Cross Origin Test</title>
     </head>
     <script>
         function getFromNet(){
             alert("here is me");
                 var httpRequest = new XMLHttpRequest();
                 var url = "http://qt.
                 //httpRequest.onreadystatechange = handleResponse;
                 httpRequest.open("GET", url, false);
                 httpRequest.setRequestHeader("Access-Control-Allow-Origin","*");
                 httpRequest.setRequestHeader("Access-Control-Allow-Methods","GET");
                 httpRequest.setRequestHeader("Access-Control-Allow-Headers","content-type");
                 httpRequest.setRequestHeader("Connection", "Close")
                 httpRequest.send();
                 alert(httpRequest.responseText);
             }
     </script>
     <body>
         <button onclick = "getFromNet()">ClickMe</button>
     </body>
 </html>

团队红星二锅头,队长王猛,领队李冲,顾问杨伟,副队张挺,外联马彪,内勤韩秀,人事顾闯,财务赵魁,后勤周强
2018-08-25 14:34
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:7 
示例

<!DOCTYPE HTML>
<html>
    <head>
        <title>Cross Origin Test</title>
    </head>
    <script type="text/javascript">
        var httpRequest;

        function getFromNet(url)
        {
            httpRequest = null;
            httpRequest = new XMLHttpRequest();
            if (httpRequest != null)
            {
                httpRequest.onreadystatechange = handleResponse;
                httpRequest.open("GET", url, true);
                httpRequest.send();
            }
        }

        function handleResponse()
        {
            if (httpRequest.readyState==4)  // loaded
            {
                if (httpRequest.status==200)  //OK
                {
                    alert(httpRequest.responseText);
                }
            }
        }
    </script>
    <body>
        <button onclick = "getFromNet('http://qt.')">ClickMe</button>
    </body>
</html>
2018-08-25 15:14
wanguhssheng
Rank: 2
等 级:论坛游民
威 望:2
帖 子:131
专家分:24
注 册:2016-2-26
得分:0 
吹佬的代码在手机上用UC浏览器实现了,红佬的没得到结果,我估计是我的手机速度的问题,谢谢了,我爱你们。
另对红佬:这个问题我在本论坛WEB前端贴了三天,可惜没人解答。
2018-08-25 16:33
wanguhssheng
Rank: 2
等 级:论坛游民
威 望:2
帖 子:131
专家分:24
注 册:2016-2-26
得分:0 
在遇到类似的问题,应该在哪个版块提问呢。。。
2018-08-25 16:35
红星二锅头
Rank: 8Rank: 8
等 级:贵宾
威 望:46
帖 子:491
专家分:754
注 册:2016-8-25
得分:0 
如果红佬的没得到结果,把 httpRequest.open("GET", url, true);
改成 httpRequest.open("GET", url, false);
同步方式试试看,,,

团队红星二锅头,队长王猛,领队李冲,顾问杨伟,副队张挺,外联马彪,内勤韩秀,人事顾闯,财务赵魁,后勤周强
2018-08-25 17:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-489415-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028140 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved