【问题标题】:cross domain ajax query without answer没有答案的跨域ajax查询
【发布时间】:2020-01-14 07:33:06
【问题描述】:

尝试做一个简单的跨源请求。使用 ajax-cross-origin 并设置我自己的“代理”来完成这项工作。奇怪的是我没有收到任何答案,成功消息也不是错误。

        $.ajax({
            crossOrigin: true,
            proxy: 'proxylink.php',
            url: url,
            type: 'GET',
            success: function(res) {
                //var headline = $(res.responseText).find('title').text();
                $('#success').html(res.responseText).text();
            },
            error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
            $('#error').html(msg);
        },
        }

我的代理文件:

<?php
$url = (isset($_GET['url'])) ? $_GET['url'] : false;
if(!$url) exit;

$referer = (isset($_SERVER['HTTP_REFERER'])) ? strtolower($_SERVER['HTTP_REFERER']) : false;
$is_allowed = $referer && strpos($referer, strtolower($_SERVER['SERVER_NAME'])) !== false; //deny abuse of your proxy from outside your site

$string = ($is_allowed) ? utf8_encode(file_get_contents($url)) : 'You are not allowed to use this proxy!';
$json = json_encode($string);
$callback = (isset($_GET['callback'])) ? $_GET['callback'] : false;
if($callback){
    $jsonp = "$callback($json)";
    header('Content-Type: application/javascript');
    echo $jsonp;
    exit;
}
echo $json;
?>

如果我在success 中执行console.log 或alert(对于字符串),它会在控制台中写入,所以我假设ajax 调用正在返回成功。但我无法处理 res 返回的值。

有什么线索吗? 谢谢。

【问题讨论】:

  • res 的值是多少?同时删除crossOrigin,因为它不需要
  • 这就是问题所在,它没有返回任何值。但是我发送给ajax调用的链接是正确的并检查了它。我不知道如何处理这个“错误”以及如何知道发生了什么。
  • 您说它写入控制台,那么那里显示的值是多少?
  • 对不起,那是我的错。我已经纠正了我的说法。我的意思是,如果我在 success 函数中进行类似 alert("Hello") 的操作,它会起作用,所以我假设它返回的是成功而不是错误。但是 res var 是空的。
  • 在这种情况下,听起来问题出在您的proxylink.php 文件中,而不是JS。另请注意,proxy 不是$.ajax 的标准属性,因此您可能仍会直接调用外部域并遇到 CORS 错误。您需要手动实现您的代理 URL

标签: jquery cross-domain


【解决方案1】:

根据您的proxylink.php,我认为您应该这样使用它:

       $.ajax({
        crossOrigin: true,
        url: 'proxylink.php',
        data: {url:url},
        type: 'GET',
        success: function(res) {
            //var headline = $(res.responseText).find('title').text();
            $('#success').html(res.responseText).text();
        },
        error: function (jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        $('#error').html(msg);
    },
    }

【讨论】:

  • 还是一样。没有任何输出。
  • 如果你删除了 crossOrigin:true ?
  • corssorigin 来自插件本身。需要启用它才能发出请求并使用代理。
猜你喜欢
  • 2012-07-08
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 2011-07-12
  • 2012-10-27
  • 2017-01-30
相关资源
最近更新 更多