使用 jQuery 的简单 AJAX HTTP GET
Simple AJAX HTTP GET using jQuery
我是 jQuery 和 AJAX 的新手,我正在尝试以下代码,只是简单的 http get 请求。
<html>
<head>
</head>
<body>
<script src = "jquery-2.1.4.js"></script>
<script src = "app.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("http://google.com", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
我收到以下错误
XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access.
请帮我看看我遗漏了什么。
同源策略不允许您访问托管在不同服务器上的资源。在您的情况下,如果目标系统不受您的控制,您可以查看像 jsonp 这样的实用程序,它可以帮助您触发跨域请求。
因此,根据同源策略 SOP 在该策略下,Web 浏览器允许第一个网页中包含的脚本访问第二个网页中的数据,但前提是两个网页具有相同的来源 same origin表示相同的 URL Scheme 、主机名和端口号。要克服这个问题,你可以使用 like
$.ajax({
url: "http://google.com",
dataType: "jsonp"},
success: function( response ) {
console.log( response ); // server response
}
})
阅读更多关于 JSONP 的内容。
这只是您从应用程序发出的跨域请求,您必须确保避免此错误,
1) Enable cors in your application to make a cross domain application.
2) You can use JSONP while making your request to cross domain instead of GET.
请仔细阅读此 link 您将获得有关 "CORS" 的更多详细信息
https://developers.google.com/api-client-library/javascript/features/cors
我是 jQuery 和 AJAX 的新手,我正在尝试以下代码,只是简单的 http get 请求。
<html>
<head>
</head>
<body>
<script src = "jquery-2.1.4.js"></script>
<script src = "app.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("http://google.com", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
我收到以下错误
XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access.
请帮我看看我遗漏了什么。
同源策略不允许您访问托管在不同服务器上的资源。在您的情况下,如果目标系统不受您的控制,您可以查看像 jsonp 这样的实用程序,它可以帮助您触发跨域请求。
因此,根据同源策略 SOP 在该策略下,Web 浏览器允许第一个网页中包含的脚本访问第二个网页中的数据,但前提是两个网页具有相同的来源 same origin表示相同的 URL Scheme 、主机名和端口号。要克服这个问题,你可以使用 like
$.ajax({
url: "http://google.com",
dataType: "jsonp"},
success: function( response ) {
console.log( response ); // server response
}
})
阅读更多关于 JSONP 的内容。
这只是您从应用程序发出的跨域请求,您必须确保避免此错误,
1) Enable cors in your application to make a cross domain application.
2) You can use JSONP while making your request to cross domain instead of GET.
请仔细阅读此 link 您将获得有关 "CORS" 的更多详细信息 https://developers.google.com/api-client-library/javascript/features/cors