ie浏览器的jQuery getJSON不能工作

Internet Explorer jQuery getJSON not working

本文关键字:不能 工作 getJSON jQuery 浏览器 ie      更新时间:2023-09-26

我正在制作一个小脚本,通过php使用jquery的getJSON方法从数据库中获取一些数据。

的代码如下:

$(document).ready(function(){
var id = userid;
$('#a-div').after('<div id="data"></div><input type="button" id="getdata" value="Get Data">');
       $('#getdata').click(function(){
        $.getJSON('http://mysite.com/data.php?id=' + id, function(data) {
           var notfound = data['notfound'];
           var user = data['user'];
           if(notfound == '1'){      
             $('#data').html("Not found");
           } 
           else{   
            $('#data').html("Found , user is "+ user);
           }
         });//end of getJSON
       });//end of click
 }); //end of document ready

我的php脚本返回JSON数据如下所示:如果在数据库中找到数据-

{"notfound":"0","user":"john"}

如果数据库中没有数据-

{"notfound":"1","user":"none"}

这在Firefox, Google Chrome和Safari上工作完美,只是在Internet explorer(7,8,9)中不起作用。有谁能帮我一下吗?

p/S我在其他类似的帖子中尝试了一些技术,但不起作用。比如改变META内容类型

谢谢。

尝试将按钮放到 body>像这样:

 $("body").append( /* markup for div and btn here */ );

…代替after().

也许ie浏览器需要很长时间来添加html,尝试使用live

$('#getdata').live('click', function(){

你正在使用非常丑陋的代码,在正文不正确之后编写任何HTML内容,为此你必须在正文中添加任何div并将你的cod附加到它,然后这段代码将在ie中工作

$(document).ready(function () {
    $('#anydiv').html('<div id="data"></div><input type="button" id="getdata" value="Get Data">');
    $('#getdata').click(function () {
        alert("asd");
        $.getJSON('/HowItWork/Index?id=' + 23, function (data) {
            var notfound = data['notfound'];
            var user = data['user'];
            if (notfound == '1') {
                $('#data').html("Not found");
            }
            else {
                $('#data').html("Found , user is " + user);
            }
        }); //end of getJSON
    }); //end of click
}); //end of document ready

这个适合我- jQuery.support.cors = true;

查找更多讨论http://jquery.10927.n7.nabble.com/jQuery-getJSON-response-not-working-with-IE-works-perfectly-in-firefox-safari-td81254.html