jQuery $ .ajax静默失败,没有错误消息,服务器响应200 OK

我即将解决这个问题。 我使用非常简单的jQuery ajax调用来从数据库中获取值,并使用值填充一些select元素,所有这些都返回为JSON。 它在大多数浏览器上无缝地为我工作,但客户端报告他们和他们的客户都没有看到结果。

我一路上添加了一些Console.log()命令以确保代码正在执行,而且确实如此。 有时ajax GET到有问题的URL工作,其他时候是STILL返回200 OK但代码根本不执行进一步,并且错误回调中没有显示ajax错误消息。

这是我正在使用的代码,有人可以发现一些可能导致某些浏览器窒息的明显问题吗? 如果是这样,如果你能指出它,我将不胜感激:

var $j = jQuery.noConflict(true); $j(document).ready(function(){ //console.log("jQuery has loaded"); //console.log("attempting to load country list via AJAX call now"); $j.ajax({ url: 'http://www.topplaces.co.za/templates/seb_one/positions/search_establishments_filter/search/db.php?q=countries&rand='+Math.random(), success: function(data){ //console.log("Successfully got country list, going to populate the dropdown now"); if(data.length){ $j("#country").children("option:not(:first)").remove(); $j("#country").attr("disabled", false); $j.each(data, function(resultIndex, result){ var o = new Option(); $j(o).html(result.country).val(result.country); $j("#country").append(o); }) //console.log("Country list should be populated now?"); } }, error: function (xhr, ajaxOptions, thrownError){ //console.log(xhr.responseText); console.log(thrownError); }, dataType: 'json', cache: false }) $j("#country").live('change', function(){ var id = $j(this).val(); if(id == ""){ $j("#province").attr("disabled", "disabled"); $j("#town").attr("disabled", "disabled"); return false; } $j.ajax({ url: 'http://www.topplaces.co.za/templates/seb_one/positions/search_establishments_filter/search/db.php?q=provinces&c='+id+'&rand='+Math.random(), success: function(data){ if(data.length){ $j("#province").children("option:not(:first)").remove(); $j("#province").attr("disabled", false); $j.each(data, function(resultIndex, result){ var o = new Option(); $j(o).html(result.province).val(result.province); $j("#province").append(o); }) } }, dataType: 'json', cache: false }) }); $j("#province").live('change', function(){ var id = $j(this).val(); if(id == ""){ $j("#town").attr("disabled", "disabled"); return false; } $j.ajax({ url: 'http://www.topplaces.co.za/templates/seb_one/positions/search_establishments_filter/search/db.php?q=towns&p='+id+'&rand='+Math.random(), success: function(data){ if(data.length){ $j("#town").children("option:not(:first)").remove(); $j("#town").attr("disabled", false); $j.each(data, function(resultIndex, result){ var o = new Option(); $j(o).html(result.town).val(result.town); $j("#town").append(o); }) } }, dataType: 'json', cache: false }) }); }) 

我已经注释掉了Consol.log命令,因为没有控制台,客户端在IE上收到错误消息。

编辑:我没有提到这是一个相同的域请求,因此遵守同源策略

完整的站点位于: http : //www.topplaces.co.za/右侧是一个动态选择组,以country开头并启动AJAX调用,直到选择省为止。 这个问题,很多人都说国家不为他们装货……

亲切的问候,西蒙

检查您的服务器应用程序是否始终返回有效的JSON对象,否则将不会接受它,因为您设置了dataType: 'json' 。 在这种情况下,将执行errorfunction而不是success

删除dataType参数并查看会发生什么,尝试使用$.parseJSON()解析传入的数据 – 如果JSON无效,它将抛出exception。

我试过你的网站,但没有省正在加载。 Json是空的。 我尝试直接访问php,它也返回空。 你检查过你的剧本了吗?

URL被称为http://www.topplaces.co.za/templates/seb_one/positions/search_establishments_filter/search/db.php?q=provinces&c=Zambia&rand=0.12686952343210578&_=1335360594228

这是参数:

 q:provinces c:Zambia rand:0.12686952343210578 _:1335360594228 

Json结果:[]

它真的是随机的所以我敢打赌它是PHP脚本没有返回json。

我也遇到过这个浏览器问题,json从ajax调用返回。 问题是我不得不在IE中查看返回数据的不同部分。 对于Firefox, data.text undefined所以我不得不使用data.documentElement.firstChild来查找json:

 var list = typeof data.text === 'undefined' ? jQuery.parseJSON(jQuery(data.documentElement.firstChild).text()) : jQuery.parseJSON(data.text);