同一个域策略的jQuery [英] Same domain policy jQuery
问题描述
我问过关于为什么大多数AJAX解决方案涉及如PHP一些后端语言的问题。
I had asked a question regarding why most ajax solutions involve some back-end language like PHP.
我被告知这是因为这样一个事实:Web浏览器不允许一个完整的JavaScript / jQuery的解决方案,因为同一个域策略。然而,下面绝对是code罚款运行:
I was told it was because of the fact that a web browser does not allow a complete javascript/jquery solution due to same domain policy. Yet the code below absolutely runs fine:
<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>
因此,这是code不应该跑?我不明白这一点。
So is this code not supposed to run? I don't get it.
谢谢, 吉姆
推荐答案
只有JSONP请求工作跨域。你做这件事的方式是正确的!
only jsonp requests work cross-domain. Your way of doing it is correct!
1),您将无法访问其他域DOM元素或JavaScript对象(在IFRAME为例) 在这里看到我的回答:<一href="http://stackoverflow.com/questions/7617429/how-can-i-create-a-function-in-another-frame/7657264#7657264">How我可以在另一帧创建一个函数?
1) you can't access DOM elements or JavaScript Objects on other domain (in iframe for example) see my answer here: How can I create a function in another frame?
2)我们用来做这样的事情(见我的回答)从JS沟通,PHP和背部。 <一href="http://stackoverflow.com/questions/1678214/javascript-how-do-i-create-jsonp/5435042#5435042">JavaScript:如何创建JSONP?
2) we used to do things like this (see my answer) to communicate from JS to PHP and back. JavaScript: How do I create JSONP?
这篇关于同一个域策略的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!