jquery ajax从http url获取响应文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1152692/
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 ajax get responsetext from http url
提问by stoimen
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
?
回答by stoimen
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);
回答by ghbarratt
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.
正如卡里姆所说,除非服务器允许,否则跨域 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/google.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 中连接到那个而不是 Google 的域:
var response = $.ajax({ type: "GET",
url: "/ajax_responders/google.php",
async: false
}).responseText;
alert(response);
回答by Ken Egozi
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.
“script”:将响应作为 JavaScript 进行评估,并将其作为纯文本返回。除非使用选项“缓存”,否则禁用缓存。注意:这会将 POST 转换为远程域请求的 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 块中。会添加一个额外的“?callback=?” 到 URL 的末尾以指定回调。(在 jQuery 1.2 中添加)
"text": A plain text string.
“text”:纯文本字符串。
回答by Not Available
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/the-jquery-cross-domain-ajax-guide)
回答by Marco Pavan
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:
要以文本形式获取外部网页的 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
}
});
回答by gustaf
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
实际上,您可以使用 IE Firefox 进行跨域请求,请参阅此概述:http: //ajaxian.com/archives/cross-site-xmlhttprequest-in-firefox-3
Webkit and IE8 supports it as well in some fashion.
Webkit 和 IE8 也以某种方式支持它。
回答by KannarKK
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 文件 responder.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 请求应该发送到这个 responder.php 文件,以便它执行跨域请求。
回答by Oleg Ruscinski
try this
尝试这个
alert( data['responseText'] );
回答by MANA624
This is super old, but hopefully this helps somebody. I'm sending responses with different error codes back and this is the only solution I've found that works in
这是超级旧的,但希望这对某人有所帮助。我正在发送带有不同错误代码的响应,这是我发现的唯一有效解决方案
$.ajax({
data: {
"data": "mydata"
},
type: "POST",
url: "myurl"
}).done(function(data){
alert(data);
}).fail(function(data){
alert(data.responseText)
});
Since JQuery deprecated the success
and error
functions, it's you need to use done
and fail
, and access the data with data.responseText
when in fail
, and just with data
when in done
. This is similar to @Marco Pavan 's answer, but you don't need any JQuery plugins or anything to use it.
由于 JQuery 不推荐使用success
anderror
函数,因此您需要使用done
and fail
,并使用data.responseText
when infail
和仅使用data
when in访问数据done
。这类似于@Marco Pavan 的答案,但您不需要任何 JQuery 插件或任何东西来使用它。