简体   繁体   English

jQuery获取HTTP URL请求

[英]jQuery Get Request on HTTP URL

i've recently tried to get some Response from an URL using jQuery. 我最近尝试使用jQuery从URL获取一些响应。 Therefore I copied a get request sample of jQuery API Get Request Tutorial into my project and tried to run it, but my debugging messages showed me, that it can't go further. 因此,我将jQuery API Get Request Tutorial的get请求示例复制到我的项目中并尝试运行它,但是我的调试消息向我显示,它无法继续下去。 I tried the javascript Ajax Library using a simple request, but it didn't work. 我使用简单的请求尝试了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. 我可能忘了包含一个ajax或jQuery库。 For a better understanding, i have c and obj-c experince, this is why i think, that a library is missing. 为了更好地理解,我有c和obj-c经验,这就是我认为库缺失的原因。

In each sample there is just a short url like "test.php". 在每个样本中只有一个简短的网址,如“test.php”。 Is my complete HTTP url wrong? 我的完整HTTP网址是错误的吗?

Thanks for your answers in advanced. 谢谢你的高级答案。

Br Nic Br Nic

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!');
      }
    }
  });
});

You're hitting the Same Origin Policy with 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. 简而言之,默认情况下,JS / Ajax只允许在与提供HTML页面的域相同的域上触发请求。 If you intend to fire requests on other domains, it has to support JSONP and/or to set the Access-Control headers in order to get it to work. 如果您打算在其他域上触发请求,则必须支持JSONP和/或设置Access-Control标头以使其正常工作。 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). 如果这不是一个选项,那么你必须在服务器端创建一些代理并改为使用它(小心,因为你可以禁止使用机器人从其他站点过度使用)。

As others have said you can't access files on another server. 正如其他人所说,你无法访问另一台服务器上的文件。 There is a hack tho. 有一个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! 这应该工作!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM