29

两者都不:

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

也不:

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

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

4

10 回答 10

36

您只需像这样重写它:

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

alert(response);
于 2009-07-20T11:05:48.017 回答
28

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

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

使用以下命令创建文件 web_root/ajax_responders/google.php:

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

然后更改您的代码以连接到该代码,而不是直接在 javascript 中连接到 Google 的域:

var response = $.ajax({ type: "GET",   
                        url: "/ajax_responders/google.php",   
                        async: false
                      }).responseText;
alert(response);
于 2011-10-23T01:40:54.397 回答
4

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

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

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

要以文本形式获取外部网页的 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
    }
});
于 2011-12-07T15:05:15.203 回答
3

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

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

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

来自文档:

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

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

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

“html”:以纯文本形式返回 HTML;包含的脚本标签在插入 DOM 时进行评估。

“脚本”:将响应评估为 JavaScript,并将其作为纯文本返回。除非使用选项“缓存”,否则禁用缓存。注意:这会将 POST 转换为远程域请求的 GET。

“json”:将响应评估为 JSON 并返回一个 JavaScript 对象。

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

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

请参阅http://docs.jquery.com/Ajax/jQuery.ajax#options

于 2009-07-20T10:13:27.780 回答
2

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

这是一个帖子,发布了一些实现跨域ajax的各种技术(http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide

于 2010-08-06T08:20:46.747 回答
1

实际上,您可以使用 Firefox 进行跨域请求,请参阅此概览:http ://ajaxian.com/archives/cross-site-xmlhttprequest-in-firefox-3

Webkit 和 IE8 也以某种方式支持它。

于 2010-08-06T08:37:59.013 回答
0

由于 jQuery AJAX 请求在跨域时会失败,因此您可以使用 cURL(在 PHP 中)设置代理服务器。

假设 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;

您的 AJAX 请求应该发送到这个 responder.php 文件,以便它执行跨域请求。

于 2015-04-19T12:33:09.017 回答
0

这个怎么样?

$.get(some-url, some-data, function(d, s, x){
    // d is text response data
    // s is text status
    // x is a XMLHttpRequest object
    // so... you can code like that:

    console.log(x.responseText);
})

享受!:)

于 2021-07-28T21:03:53.110 回答
-1

这是超级旧的,但希望这可以帮助某人。我正在发送带有不同错误代码的响应,这是我发现的唯一可行的解​​决方案

$.ajax({
    data: {
        "data": "mydata"
    },
    type: "POST",
    url: "myurl"
}).done(function(data){
    alert(data);
}).fail(function(data){
    alert(data.responseText)
});

由于 JQuery 不推荐使用successanderror函数,因此您需要使用doneand并使用when in和仅使用when infail访问数据。data.responseTextfaildatadone

于 2018-07-14T17:57:58.557 回答
-2

尝试这个

alert( data['responseText'] );
于 2016-08-13T19:14:48.053 回答