jQuery的$就默默地失败,没有错误信息,并与200 OK服务器响应 [英] jQuery $.ajax failing silently, no error messages, and server responded with 200 OK

查看:304
本文介绍了jQuery的$就默默地失败,没有错误信息,并与200 OK服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将做我的头了这个问题。我使用非常简单的jQuery AJAX调用从数据库中获取值和填充值的一些特定元素,所有返回的JSON。它可以无缝地对我大多数浏览器,但客户端报告说,他们既不和他们的客户所看到的结果。

I am about to do my head in over this problem. I am using very simple jQuery ajax calls to get values from a database and populate a few select elements with the values, all returned as JSON. It works seamlessly for me on most browsers, however the client is reporting that neither them nor their clients are seeing the result.

我加了一些CONSOLE.LOG()沿,以确保code的执行命令的方式,它是。有时,AJAX到有问题的作品,其他时间还是将URL返回200 OK,但code根本不会进一步执行,NO Ajax错误消息显示在错误回调。

I added some Console.log() commands along the way to make sure the code was executing, and it was. Sometimes the ajax GET to the URL in question works, other times is STILL returns 200 OK but the code simply does not execute further, and NO ajax error messages are shown in the error callback.

下面是code我用的,可有人点了一些东西明显,可能会导致某些浏览器窒息吗?如果是这样,我会很感激,如果你能指出来:

Here is the code I am using, can someone spot something obvious that may result in some browsers choking? If so, I'd be grateful if you could point it out:

        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
                })
            });

        })

我已经注释掉了纯粹的事实是,客户端已在IE浏览器收到错误消息,因为没有控制台consol.log的命令。

I have commented out the Consol.log commands for the pure fact that the client was receiving error messages on IE as there is no console.

编辑:我没有提到这同一个域名的请求,因此服从同源策略

整个网站是在这里: http://www.topplaces.co.za/ 在右边的是,随着国家启动并开始AJAX调用,直到一个省选择一个动态的选择组。这个问题,很多人说,国家是没有加载它们...

The full site is here: http://www.topplaces.co.za/ On the right is a dynamic select group that starts with country and initiates AJAX calls until a Province is selected. The issue, a lot of people say that Country is no loading for them...

亲切的问候, 西蒙

推荐答案

请确认您的服务器应用程序总是返回有效的JSON对象,否则,因为你把它设置将不被接受数据类型:JSON。在这种情况下,错误功能将被执行,而不是成功

Check if your server application always returns valid JSON object, otherwise it will not be accepted because you set dataType: 'json'. In this case, error function will be executed instead of success.

删除的dataType 参数,看看会发生什么,尝试解析与 $传入数据parseJSON() - 它如果你JSON无效,将引发异常。

Remove dataType parameter and see what happens, try to parse incoming data with $.parseJSON() - it will throw an exception if you JSON is invalid.

这篇关于jQuery的$就默默地失败,没有错误信息,并与200 OK服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆