Javascript 相同域策略jQuery
我问了一个问题,为什么大多数ajax解决方案都涉及一些后端语言,比如PHP 有人告诉我,这是因为,由于相同的域策略,web浏览器不允许使用完整的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
<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。