javascript - 使用 Jquery 从 url 获取响应

标签 javascript html http jquery

我正在使用 jquery 发出 GET 请求,但没有得到响应,这可能是什么问题? 下面是html代码。我收到一条警报,显示

请任何人为我提供一些运行示例,其中应该使用一些我也可以访问的知名网站,例如 http://www.google.co.inhttp://www.jlcindia.com

error:error

<html>
<script type="text/javascript" src="jquery-1.8.1.min.js"></script>
<Script Language="JavaScript">

function getFullName(){
            //alert('fun call');

                $.ajax({
                    type: "GET",
                    url: "http://www.jlcindia.com",
                    dataType: "html",
                    success: function(data) {
                        alert('success'+data);
                    },
                    error: function(xhr, textStatus, errorThrown) {
                                if (textStatus !== null) {
                                    alert("error: " + textStatus);
                                } else if (errorThrown !== null) {
                                    alert("exception: " + errorThrown.message);
                                }
                                else {
                                    alert ("Don't know what is error");
                                }
                            }
                });               
        }
</Script>
<body>
<input type="button" value="Show Full Name" onClick="getFullName()">
</body>
</html>

最佳答案

您正在向不同的域发出 ajax 请求。由于same origin policy,这将不起作用

示例: 您的网站托管在 www.mydomain.com 上,并且您正在尝试通过 ajax 访问 www.otherdomain.com。这是不可能的。

Ajax 请求只能在与托管脚本相同的域中工作。

关于javascript - 使用 Jquery 从 url 获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478258/

相关文章:

javascript - 仅当单词唯一时才匹配该单词

java - html5检查器编译

http - Golang,发送OCSP请求返回

ios - 从 HTTP JSON 树中接收特定信息

javascript - 使用 ng-submit 提交数组数据报错 Cannot read property 'name' of undefined

javascript - 谷歌地图无限线/增加长度或纬度lng计算

javascript - jqplot从表中设置数据系列

html - Css 在屏幕缩小时创建全宽 div

javascript - 道场 : Accordion open and close

ruby-on-rails - 使用 JSON 和 POST 将我的 iOS 应用程序连接到 Web 应用程序问题