【问题标题】:url ok in browser and curl, times out in ajaxurl 在浏览器和 curl 中正常,在 ajax 中超时
【发布时间】:2014-08-03 06:30:43
【问题描述】:

我正在尝试让 Geobytes ajax 城市搜索在我的服务器上运行。我已经从http://www.geobytes.com/free-ajax-cities-jsonp-api.htm 中剪切并粘贴了代码,但GetJSON 例程没有做任何事情; .done()、.error() .. 甚至回调都没有。

我插入了一个 .ajax-check,它告诉我有一个超时错误。但是当我将来自 console.log() 的 URL 粘贴到浏览器中时,它工作正常。同样的 URL 在 PHP curl 服务器端也可以正常工作。

知道什么可能是错的吗?

(Geobytes 论坛似乎没有维护,我的帖子从未出现过)

谢谢

<script type="text/javascript">

jQuery(function () 
 {
     jQuery("#f_elem_city").autocomplete({
        source: function (request, response) {
         console.log("searching for http://gd.geobytes.com/AutoCompleteCity?callback=?&q="+request.term);
         $.ajax({
            url: "http://gd.geobytes.com/AutoCompleteCity?callback=?&q="+request.term,
            dataType: 'json',
            timeout: 8000,
            success: function( data, string ) { console.log("success"); console.log(string);},
            error: function( data, string ) { console.log("error"); console.log(string); }
        });
        console.log("after ajax");
         jQuery.getJSON(
            "http://gd.geobytes.com/AutoCompleteCity?callback=?&q="+request.term,
            function (data) {
                console.log("success");
             response(data);
            }
         );
        },
        minLength: 3,
        select: function (event, ui) {
         var selectedObj = ui.item;
         jQuery("#f_elem_city").val(selectedObj.value);
         return false;
        },
        open: function () {
         jQuery(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
         jQuery(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
     });
     jQuery("#f_elem_city").autocomplete("option", "delay", 100);
    });
</script>

<form action="" method="post" name="form_demo" id="form_demo" enctype="multipart/form-data" onsubmit="return false;">
<p><b>Please enter</b> your city here to see it work. <input class="ff_elem" type="text" name="ff_nm_from[]" value="" id="f_elem_city"/>
</form>

【问题讨论】:

    标签: jquery ajax jsonp


    【解决方案1】:

    尝试为跨域 ajax 请求添加 JSONP。 因此dataType: 'json' 必须是dataType: 'jsonp'

    【讨论】:

      【解决方案2】:

      我有一个类似的问题,它是跨域请求。当我在响应服务器中添加“Access-Control-Allow-Origin”标头时,问题得到解决。

      ajax GET request times out for URL when browser and CURL work

      由于您似乎在使用第三方服务器,我不确定这是否可行。

      【讨论】:

        猜你喜欢
        • 2015-09-02
        • 2014-01-13
        • 2012-07-11
        • 1970-01-01
        • 2019-11-01
        • 2017-12-11
        • 2018-05-19
        • 2017-06-16
        • 1970-01-01
        相关资源
        最近更新 更多