IE10-jQuery ajax html无法加载数据 [英] IE10 - JQuery ajax html not loading data

查看:144
本文介绍了IE10-jQuery ajax html无法加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过查看以下帖子,我已经进行了一些研究( Ajax在IE10上不起作用)和其他一些网站,但是很遗憾,它并没有解决我的问题,这就是为什么我需要您的帮助;)

I have done some research already by looking at the following post (Ajax not work on IE10) and few other websites, but unfortunately it did not fixed my problem and that's why I need your help ;)

在下面您可以看到我的JQuery ajax脚本,我的问题是可以进入ajax并将值返回给errorDivStyle,但是我无法加载$(#mainView").html(data);数据从PHP脚本及其HTML返回.

Below you can see my JQuery ajax script, My problem is that I can get to ajax and return value to errorDivStyle, but I am not able to load $("#mainView").html(data); data is returned from PHP script and its HTML.

请注意,它在Chrome,FF和IE 9中工作正常,但不适用于IE10.我在做错什么吗?

Please note it works perfectly fine in Chrome, FF and IE 9 but NOT IE10. Is there something I am doing wrong ??

<script type="text/javascript">
        $(document).ready(function () {
        var category = $("#category").val();
        $("#category").change(function(){
            $.ajax({
                type: "POST",
                url: "test.php",
                data: $(this).serialize(),
                cache: false,
                success: function(data) {
                    if(data)
                    {
                        $("#mainView").html(data);
                        $("#errorDivStyle").html("<font color='#000000'>" + category + " category have been loaded</font>").show();
                    }
                }
            });
            return false;
        });
    });
    </script>

推荐答案

我没有发现您的代码有任何公然错误.

I see nothing blatantly wrong with your code.

如果您使用的是Jquery的较新版本,则建议更改:

If you are using a newer version of Jquery then I would suggest changing:

$("#category").change(function(){

进入

$("#category").on('change', function(){

当然也可以

cache: false,

添加

dataType: 'html',

我也强烈建议使用console.log()

Also I highly recommend using console.log()

if(data)
{
    //check if expected data is correct
    console.log(data);
    //make sure you can target #mainview properly
    //should output the amount of #mainviews on your screen
    console.log($("#mainView").length);

    $("#mainView").html(data);
    $("#errorDivStyle").html("<font color='#000000'>" + category + " category have been loaded</font>").show();
}

    $("#category").on('change', function(){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "test.php",
            data: $(this).serialize(),
            cache: false,
            dataType: 'html',
            success: function(data) {
                if(data)
                {
                    $("#mainView").html(data);
                    $("#errorDivStyle").html("<font color='#000000'>" + category + " category have been loaded</font>").show();
                }
            }
        });
    });

您为什么仍要返回FALSE?我刚刚意识到,这可能是您正在侦听的文本字段更改.因此e.preventDefault()也不是必需的...

Why were you returning FALSE anyways? I just realized that this is probably a text field change that you are listening for. So the e.preventDefault() isn't necessary either...

此外,如果您只想将单个值传递给test.php,则可以尝试以下操作:

Also if you just want to pass a single value to your test.php then you can try this:

    $("#category").on('change', function(){
        $.ajax({
            url: "test.php?q="+$(this).val(),
            cache: false,
            dataType: 'html',
            success: function(data) {
                if(data)
                {
                    $("#mainView").html(data);
                    $("#errorDivStyle").html("<font color='#000000'>" + category + " category have been loaded</font>").show();
                }
            }
        });
    });

递归地修剪数组

/**
* Returns a recursively TRIMMED variable
*
* @access   public
* @param    mixed
* @return   mixed
*/
if ( ! function_exists('trim_r'))
{
    function trim_r($var)
    {
        //In here you can add more code to handle objects properly but I did not because I am not using them with this function
        //if (is_array($var) || is_object($var)){some code}
        if (is_array($var))
        {
            return array_map('trim_r', $var);
        }
        else
        {
            return trim($var);
        }
    }
}


// Usage examples
$_POST = trim_r($_POST);
$_GET = trim_r($_GET);
$_REQUEST = trim_r($_REQUEST);
$custom_array = trim_r($custom_array);

这篇关于IE10-jQuery ajax html无法加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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