jQuery获取HTTP URL请求

Nic*_*sla 7 javascript ajax jquery get httprequest

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

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

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

var url = "http://www.google.com";

$.get(url, function(data){


    alert("Data Loaded: " + data);
    });
Run Code Online (Sandbox Code Playgroud)

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

在每个样本中只有一个简短的网址,如"test.php".我的完整HTTP网址是错误的吗?

谢谢你的高级答案.

Br Nic

Oli*_*ryn 13

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

<!-- 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
Run Code Online (Sandbox Code Playgroud)

这可能最好包含在外部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!');
      }
    }
  });
});
Run Code Online (Sandbox Code Playgroud)


Bal*_*usC 9

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

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

  • 只需更改服务器的URL即可.例如`$ .get('leech.php?url ='+ encodeURIComponent('http://www.google.com'))`.但更好的方法是利用Google提供的公共ajax API(或您打算连接的任何主机).例如`http://ajax.googleapis.com/ajax/services/search/web?v = 1.0&q = your + search + term` (3认同)