jQuery获取HTTP URL请求

我最近尝试使用jQuery从URL获取一些响应。 因此,我将jQuery API Get Request Tutorial的get请求样本复制到我的项目中并尝试运行它,但是我的调试消息向我显示,它无法继续下去。 我使用简单的请求尝试了javascript Ajax库,但它没有用。

所以我问你,如果你能以某种方式帮助我。

这就是我所做的一切,但没有回应。

var url = "http://www.google.com"; $.get(url, function(data){ alert("Data Loaded: " + data); }); 

我可能忘了包含ajax或jQuery库。 为了更好地理解,我有c和obj-c经验,这就是我认为库缺失的原因。

在每个样本中只有一个简短的url,如“test.php”。 我的完整HTTPurl是错误的吗?

谢谢你的高级答案。

Br Nic

我提供了一个示例场景来帮助您入门:

   

这可能最好包含在外部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!'); } } }); }); 

您正在针对ajax请求达到同源策略 。

简而言之,默认情况下,JS / Ajax只允许在与提供HTML页面的域相同的域上触发请求。 如果您打算在其他域上触发请求,它必须支持JSONP和/或设置Access-Control标头以使其工作。 如果这不是一个选项,那么你必须在服务器端创建一些代理并改为使用它(小心,因为你可以禁止使用机器人从其他站点过多地汲取水分)。

正如其他人所说,你无法访问另一台服务器上的文件。 有一个hack tho。 如果您使用的是服务器端语言(我假设您是这样),您可以执行以下操作:

http://myserver.com/google.php

  

http://myserver.com/myscript.js

 $.get('google.php',function(data){ console.log(data) }); 

这应该工作!

您只需访问域/服务器中的页面即可