Javascript jQuery 获取 HTTP URL 上的请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5942105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Get Request on HTTP URL
提问by NicTesla
i've recently tried to get some Response from an URL using jQuery. Therefore I copied a get request sample of jQuery API Get Request Tutorialinto my project and tried to run it, but my debugging messages showed me, that it can't go further. I tried the javascript Ajax Library using a simple request, but it didn't work.
我最近尝试使用 jQuery 从 URL 获取一些响应。因此,我将jQuery API 获取请求教程的获取请求示例复制到我的项目中并尝试运行它,但我的调试消息显示,它不能再进一步了。我使用一个简单的请求尝试了 javascript Ajax 库,但没有奏效。
So i'm asking you, if you could help me somehow.
所以我问你,如果你能以某种方式帮助我。
And this is all what i do, but there is no response.
这就是我所做的一切,但没有回应。
var url = "http://www.google.com";
$.get(url, function(data){
alert("Data Loaded: " + data);
});
Did i probably forgot to include a ajax or jQuery library. For a better understanding, i have c and obj-c experince, this is why i think, that a library is missing.
我是否可能忘记包含 ajax 或 jQuery 库。为了更好地理解,我有 c 和 obj-c 经验,这就是为什么我认为缺少一个库。
In each sample there is just a short url like "test.php". Is my complete HTTP url wrong?
在每个示例中,只有一个像“test.php”这样的短网址。我的完整 HTTP url 是错误的吗?
Thanks for your answers in advanced.
感谢您的高级回答。
Br Nic
Br 尼克
回答by Oliver Spryn
I have provided an example scenario to help get you started:
我提供了一个示例场景来帮助您入门:
<!-- Include this jQuery library in your HTML somewhere: -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script
This is probably best to include inside of an external JS file:
这可能最好包含在外部 JS 文件中:
//Listen when a button, with a class of "myButton", is clicked
//You can use any jQuery/JavaScript event that you'd like to trigger the call
$('.myButton').click(function() {
//Send the AJAX call to the server
$.ajax({
//The URL to process the request
'url' : 'page.php',
//The type of request, also known as the "method" in HTML forms
//Can be 'GET' or 'POST'
'type' : 'GET',
//Any post-data/get-data parameters
//This is optional
'data' : {
'paramater1' : 'value',
'parameter2' : 'another value'
},
//The response from the server
'success' : function(data) {
//You can use any jQuery/JavaScript here!!!
if (data == "success") {
alert('request sent!');
}
}
});
});
回答by BalusC
You're hitting the Same Origin Policywith regard to ajax requests.
您在ajax 请求方面遇到了同源策略。
In a nutshell, JS/Ajax is by default only allowed to fire requests on the same domain as where the HTML page is been served from. If you intend to fire requests on other domains, it has to support JSONPand/or to set the Access-Control
headersin order to get it to work. If that is not an option, then you have to create some proxy on the server side and use it instead (be careful since you can be banned for leeching too much from other sites using a robot).
简而言之,默认情况下,JS/Ajax 只允许在与提供 HTML 页面的域相同的域上触发请求。如果您打算在其他域上触发请求,则它必须支持JSONP和/或设置Access-Control
标头才能使其正常工作。如果这不是一个选项,那么您必须在服务器端创建一些代理并使用它(请小心,因为您可能会因使用机器人从其他站点窃取过多信息而被禁止)。
回答by user400908
As others have said you can't access files on another server. There is a hack tho. If you are using a server side language (as i assume you are) you can simply do something like:
正如其他人所说,您无法访问另一台服务器上的文件。有一个黑客。如果您使用的是服务器端语言(正如我假设的那样),您可以简单地执行以下操作:
http://myserver.com/google.php:
http://myserver.com/google.php:
<?php
echo get_file_contents('http://www.google.com');
?>
http://myserver.com/myscript.js
http://myserver.com/myscript.js
$.get('google.php',function(data){ console.log(data) });
That should work!
那应该工作!
回答by ave4496
you just can access pages from your domain/server
您只能从您的域/服务器访问页面