jquery ajax从http url获取responsetext

时间:2022-10-09 00:23:00

Neither:

没有:

var response = $.ajax({
    type: "GET",   
    url: "http://www.google.de",   
    async: false,
    success : function() {
        alert (this);
    }
});

Nor:

也没有:

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

give me an object. How do I get access to the responseText?

给我一个对象。如何访问responseText?

8 个解决方案

#1


29  

You simply must rewrite it like that:

你必须把它重写成这样:

var response = '';
$.ajax({ type: "GET",   
         url: "http://www.google.de",   
         async: false,
         success : function(text)
         {
             response = text;
         }
});

alert(response);

#2


21  

As Karim said, cross domain ajax doesn't work unless the server allows for it. In this case Google does not, BUT, there is a simple trick to get around this in many cases. Just have your local server pass the content retrieved through HTTP or HTTPS.

正如Karim所说,除非服务器允许,否则跨域ajax不能工作。在这种情况下谷歌没有,但是,在很多情况下有一个简单的技巧来解决这个问题。只需让本地服务器通过HTTP或HTTPS传递检索到的内容。

For example, if you were using PHP, you could:

例如,如果你使用的是PHP,你可以:

Create the file web_root/ajax_responders/google.php with:

创建文件web_root / ajax_responders /谷歌。php:

<?php
  echo file_get_contents('http://www.google.de');
?>

And then alter your code to connect to that instead of to Google's domain directly in the javascript:

然后修改代码,连接到它,而不是在javascript中直接连接到谷歌的域:

var response = $.ajax({ type: "GET",   
                        url: "/ajax_responders/google.php",   
                        async: false
                      }).responseText;
alert(response);

#3


3  

in jquery ajax functions, the success callback signature is:

在jquery ajax函数中,成功回调签名为:

function (data, textStatus) {
  // data could be xmlDoc, jsonObj, html, text, etc...
  this; // the options for this ajax request
}

depending on the data type you've asked, using the 'dataType' parameter, you'll get the 'data' argument.

根据您要求的数据类型,使用“dataType”参数,您将获得“data”参数。

from the docs:

从文档:

dataType (String) Default: Intelligent Guess (xml or html). The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response.

默认数据类型(字符串):智能猜测(xml或html)。您期望从服务器返回的数据类型。如果没有指定,jQuery将根据响应的MIME类型智能地将responseXML或responseText传递给您的成功回调。

The available types (and the result passed as the first argument to your success callback) are:

可用的类型(以及作为成功回调的第一个参数传递的结果)是:

"xml": Returns a XML document that can be processed via jQuery.

“xml”:返回可通过jQuery处理的xml文档。

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

“html”:以纯文本的形式返回html;当插入到DOM时,将计算包含的脚本标记。

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.

“脚本”:以JavaScript计算响应并以纯文本返回。禁用缓存,除非使用“缓存”选项。注意:这将使帖子成为远程域请求的get。

"json": Evaluates the response as JSON and returns a JavaScript Object.

“json”:计算响应为json并返回一个JavaScript对象。

"jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback. (Added in jQuery 1.2)

“jsonp”:使用jsonp加载JSON块。将在URL的末尾添加一个额外的“?callback=?”来指定回调。(添加到jQuery 1.2)

"text": A plain text string.

“文本”:纯文本字符串。

see http://docs.jquery.com/Ajax/jQuery.ajax#options

看到http://docs.jquery.com/Ajax/jQuery.ajax选项

#4


2  

The only way that I know that enables you to use ajax cross-domain is JSONP (http://ajaxian.com/archives/jsonp-json-with-padding).

我所知道的使您能够使用ajax跨域的惟一方法是JSONP (http://ajaxian.com/archives/jsonp-json-with-padding)。

And here's a post that posts some various techniques to achieve cross-domain ajax (http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide)

这里有一篇文章介绍了一些实现跨域ajax的技术(http://usejquery.com/posts/9/jquerycross -domain-ajax-guide)

#5


2  

First you have to download a JQuery plugin to allow Cross-domain requests. Download it here: https://github.com/padolsey/jQuery-Plugins/downloads

首先,您必须下载一个JQuery插件以允许跨域请求。在这里下载:https://github.com/padolsey/jQuery-Plugins/downloads

Import the file called query.xdomainsajax.js into your project and include it with this code:

导入名为query.xdomainsajax的文件。将js加入到您的项目中,并包含以下代码:

<script type="text/javascript" src="/path/to/the/file/jquery.xdomainajax.js"></script>

To get the html of an external web page in text form you can write this:

要以文本形式获取外部web页面的html,可以这样写:

$.ajax({
    url: "http://www.website.com",
    type: 'GET',
    success: function(res) {
        var text = res.responseText;
        // then you can manipulate your text as you wish
    }
});

#6


1  

Actually, you can make cross domain requests with i.e. Firefox, se this for a overview: http://ajaxian.com/archives/cross-site-xmlhttprequest-in-firefox-3

实际上,您可以使用Firefox进行跨域请求,请参考以下概述:http://ajaxian.com/archives/cross-site- xmlhttprequestin - Firefox -3

Webkit and IE8 supports it as well in some fashion.

Webkit和IE8在某种程度上也支持它。

#7


0  

Since jQuery AJAX requests fail if they are cross-domain, you can use cURL (in PHP) to set up a proxy server.

由于jQuery AJAX请求如果是跨域的,就会失败,所以您可以使用cURL(在PHP中)来设置代理服务器。

Suppose a PHP file responder.php has these contents:

假设有一个PHP文件响应程序。php有这些内容:

$url = "https://www.google.com";
$ch      = curl_init( $url );
curl_set_opt($ch, CURLOPT_RETURNTRANSFER, "true")
$response= curl_exec( $ch );
curl_close( $ch );
return $response;

Your AJAX request should be to this responder.php file so that it executes the cross-domain request.

您的AJAX请求应该发送到此响应器。php文件,以便它执行跨域请求。

#8


0  

try this

试试这个

alert( data['responseText'] );

#1


29  

You simply must rewrite it like that:

你必须把它重写成这样:

var response = '';
$.ajax({ type: "GET",   
         url: "http://www.google.de",   
         async: false,
         success : function(text)
         {
             response = text;
         }
});

alert(response);

#2


21  

As Karim said, cross domain ajax doesn't work unless the server allows for it. In this case Google does not, BUT, there is a simple trick to get around this in many cases. Just have your local server pass the content retrieved through HTTP or HTTPS.

正如Karim所说,除非服务器允许,否则跨域ajax不能工作。在这种情况下谷歌没有,但是,在很多情况下有一个简单的技巧来解决这个问题。只需让本地服务器通过HTTP或HTTPS传递检索到的内容。

For example, if you were using PHP, you could:

例如,如果你使用的是PHP,你可以:

Create the file web_root/ajax_responders/google.php with:

创建文件web_root / ajax_responders /谷歌。php:

<?php
  echo file_get_contents('http://www.google.de');
?>

And then alter your code to connect to that instead of to Google's domain directly in the javascript:

然后修改代码,连接到它,而不是在javascript中直接连接到谷歌的域:

var response = $.ajax({ type: "GET",   
                        url: "/ajax_responders/google.php",   
                        async: false
                      }).responseText;
alert(response);

#3


3  

in jquery ajax functions, the success callback signature is:

在jquery ajax函数中,成功回调签名为:

function (data, textStatus) {
  // data could be xmlDoc, jsonObj, html, text, etc...
  this; // the options for this ajax request
}

depending on the data type you've asked, using the 'dataType' parameter, you'll get the 'data' argument.

根据您要求的数据类型,使用“dataType”参数,您将获得“data”参数。

from the docs:

从文档:

dataType (String) Default: Intelligent Guess (xml or html). The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response.

默认数据类型(字符串):智能猜测(xml或html)。您期望从服务器返回的数据类型。如果没有指定,jQuery将根据响应的MIME类型智能地将responseXML或responseText传递给您的成功回调。

The available types (and the result passed as the first argument to your success callback) are:

可用的类型(以及作为成功回调的第一个参数传递的结果)是:

"xml": Returns a XML document that can be processed via jQuery.

“xml”:返回可通过jQuery处理的xml文档。

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

“html”:以纯文本的形式返回html;当插入到DOM时,将计算包含的脚本标记。

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.

“脚本”:以JavaScript计算响应并以纯文本返回。禁用缓存,除非使用“缓存”选项。注意:这将使帖子成为远程域请求的get。

"json": Evaluates the response as JSON and returns a JavaScript Object.

“json”:计算响应为json并返回一个JavaScript对象。

"jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback. (Added in jQuery 1.2)

“jsonp”:使用jsonp加载JSON块。将在URL的末尾添加一个额外的“?callback=?”来指定回调。(添加到jQuery 1.2)

"text": A plain text string.

“文本”:纯文本字符串。

see http://docs.jquery.com/Ajax/jQuery.ajax#options

看到http://docs.jquery.com/Ajax/jQuery.ajax选项

#4


2  

The only way that I know that enables you to use ajax cross-domain is JSONP (http://ajaxian.com/archives/jsonp-json-with-padding).

我所知道的使您能够使用ajax跨域的惟一方法是JSONP (http://ajaxian.com/archives/jsonp-json-with-padding)。

And here's a post that posts some various techniques to achieve cross-domain ajax (http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide)

这里有一篇文章介绍了一些实现跨域ajax的技术(http://usejquery.com/posts/9/jquerycross -domain-ajax-guide)

#5


2  

First you have to download a JQuery plugin to allow Cross-domain requests. Download it here: https://github.com/padolsey/jQuery-Plugins/downloads

首先,您必须下载一个JQuery插件以允许跨域请求。在这里下载:https://github.com/padolsey/jQuery-Plugins/downloads

Import the file called query.xdomainsajax.js into your project and include it with this code:

导入名为query.xdomainsajax的文件。将js加入到您的项目中,并包含以下代码:

<script type="text/javascript" src="/path/to/the/file/jquery.xdomainajax.js"></script>

To get the html of an external web page in text form you can write this:

要以文本形式获取外部web页面的html,可以这样写:

$.ajax({
    url: "http://www.website.com",
    type: 'GET',
    success: function(res) {
        var text = res.responseText;
        // then you can manipulate your text as you wish
    }
});

#6


1  

Actually, you can make cross domain requests with i.e. Firefox, se this for a overview: http://ajaxian.com/archives/cross-site-xmlhttprequest-in-firefox-3

实际上,您可以使用Firefox进行跨域请求,请参考以下概述:http://ajaxian.com/archives/cross-site- xmlhttprequestin - Firefox -3

Webkit and IE8 supports it as well in some fashion.

Webkit和IE8在某种程度上也支持它。

#7


0  

Since jQuery AJAX requests fail if they are cross-domain, you can use cURL (in PHP) to set up a proxy server.

由于jQuery AJAX请求如果是跨域的,就会失败,所以您可以使用cURL(在PHP中)来设置代理服务器。

Suppose a PHP file responder.php has these contents:

假设有一个PHP文件响应程序。php有这些内容:

$url = "https://www.google.com";
$ch      = curl_init( $url );
curl_set_opt($ch, CURLOPT_RETURNTRANSFER, "true")
$response= curl_exec( $ch );
curl_close( $ch );
return $response;

Your AJAX request should be to this responder.php file so that it executes the cross-domain request.

您的AJAX请求应该发送到此响应器。php文件,以便它执行跨域请求。

#8


0  

try this

试试这个

alert( data['responseText'] );