Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 相同域策略jQuery_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 相同域策略jQuery

Javascript 相同域策略jQuery,javascript,jquery,ajax,Javascript,Jquery,Ajax,我问了一个问题,为什么大多数ajax解决方案都涉及一些后端语言,比如PHP 有人告诉我,这是因为,由于相同的域策略,web浏览器不允许使用完整的javascript/jquery解决方案。然而,下面的代码绝对运行良好: <script type="text/javascript"> $(document).ready(function () { $("#1").click(function () { $.aja

我问了一个问题,为什么大多数ajax解决方案都涉及一些后端语言,比如PHP

有人告诉我,这是因为,由于相同的域策略,web浏览器不允许使用完整的javascript/jquery解决方案。然而,下面的代码绝对运行良好:

<script type="text/javascript">
        $(document).ready(function () {
            $("#1").click(function () {

                $.ajax({
                    type: "GET",
                    url: "http://api.wunderground.com/api/ac7e64a2f6e2d440/geolookup/conditions/q/IA/Cedar_Rapids.json",
                    dataType: "jsonp",
                    success: function (parsed_json) {
                        $('div').html("Current temperature in " + parsed_json.current_observation.temp_f);
                        alert(parsed_json.location.city);
                        var location = parsed_json['location']['city'];
                        var temp_f = parsed_json['current_observation']['temp_f'];
                        alert("Current temperature in " + location + " is: " + temp_f);
                    }
                });

            });

        });
</script>

$(文档).ready(函数(){
$(“#1”)。单击(函数(){
$.ajax({
键入:“获取”,
url:“http://api.wunderground.com/api/ac7e64a2f6e2d440/geolookup/conditions/q/IA/Cedar_Rapids.json",
数据类型:“jsonp”,
成功:函数(已解析的_json){
$('div').html(“当前温度输入”+解析的json.Current\u observation.temp\u f);
警报(已解析的_json.location.city);
var location=parsed_json['location']['city'];
var temp_f=parsed_json['current_observation']['temp_f'];
警报(“位置+”中的当前温度为:“+temp_f”);
}
});
});
});
那么这段代码不应该运行吗?我不明白

谢谢, Jim

数据类型:“jsonp”,

JSONP用于绕过同源策略

以下是有关更多信息的链接-


另外,请注意,服务器也可以简单地允许访问。这是大多数第三方API供应商所做的。-

只有jsonp请求可以跨域工作。你的做法是正确的

1) 您不能访问其他域上的DOM元素或JavaScript对象(例如在iframe中) 请看我的回答:

2) 我们曾经做过这样的事情(见我的答案)来从JS到PHP来回通信。

感谢你们两位的澄清!特别是JSONP和JSON。