V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xmbaozi
V2EX  ›  问与答

好诡异的一段jquery,没报错,chrome、火狐不能执行,IE8居然可以

  •  
  •   xmbaozi · 2013-06-21 14:16:35 +08:00 · 5022 次点击
    这是一个创建于 3961 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $('#send_mobi_veri').click(function(){
    var mobile = document.getElementById("mobile_phone").value;
    $.getJSON("user.php?act=send_mobi_veri&mobile="+mobile,function(data){
    $("#mobi_veri_code").val('a');
    $("#send_mobi_veri").val("激活码已发送");
    });

    });

    在chrome、firefox下点击#send_mobi_veri 后可以看到get请求并返回,但是里面的代码却没执行。
    $("#mobi_veri_code").val('a');
    $("#send_mobi_veri").val("激活码已发送");
    这两句没执行。
    而IE8都正常。

    写jquery最蛋疼的,找不到错误的地方,有没有什么调试工具,或者方法。求推荐。
    16 条回复    1970-01-01 08:00:00 +08:00
    66beta
        1
    66beta  
       2013-06-21 14:26:00 +08:00   ❤️ 1
    在$("#mobi_veri_code").val('a');前面加一句alert试试

    不好意思,俺就是喜欢那么做~~
    xmbaozi
        2
    xmbaozi  
    OP
       2013-06-21 14:39:30 +08:00
    嗯。。我也喜欢那么干。
    一样没反应,没报错。IE8 可以弹出
    xmbaozi
        3
    xmbaozi  
    OP
       2013-06-21 14:40:12 +08:00
    @66beta ↑↑ 补@ 谢谢
    revlis7
        4
    revlis7  
       2013-06-21 14:48:30 +08:00   ❤️ 1
    看看返回的json数据有没有问题?如果getJSON返回数据格式有问题,好像不会触发callback。
    xmbaozi
        5
    xmbaozi  
    OP
       2013-06-21 14:56:26 +08:00
    @revlis7 返回
    {"code":0}
    没错吧 状态是200
    juicy
        6
    juicy  
       2013-06-21 15:09:04 +08:00   ❤️ 1
    @xmbaozi 返回数据格式不对的可能性比较大, 也许是编码问题, 返回的数据是utf-8 withou BOM的格式么? 如果带有BOM,可能无法正确解析
    csx163
        7
    csx163  
       2013-06-21 15:40:18 +08:00
    调试工具:chrome 按F12 点console
    我一般是用console.log()来调试,很方便
    binux
        8
    binux  
       2013-06-21 15:41:37 +08:00
    Content-Type
    yleo77
        9
    yleo77  
       2013-06-21 16:41:44 +08:00 via iPhone

    控制台没给出信息吗?
    ijse
        10
    ijse  
       2013-06-21 17:04:21 +08:00
    @xmbaozi
    $.ajax({
    url: "user.php?act=send_mobi_veri&mobile="+mobile,
    dataType: "json"
    success: function(data){
    $("#mobi_veri_code").val('a');
    $("#send_mobi_veri").val("激活码已发送");
    }
    });
    试试这样能行不? 反正我知道把`success:`换成`complete:`肯定行。

    @binux 可能是跟`Content-Type`有关,后端返回数据时应设成`text/json`
    xmbaozi
        11
    xmbaozi  
    OP
       2013-06-21 17:23:22 +08:00
    后端返回数据时应设成`text/json` 还是一样。
    在控制台执行这个,
    $.get("user.php?act=send_mobi_veri&mobile=1",function(data){
    alert('a');
    });

    返回一下错误

    Uncaught TypeError: Object function () {
    var a = ['{'], // The array holding the text fragments.
    b, // A boolean indicating that a comma is required.
    k, // The current key.
    v; // The current value.

    function p(s) {

    // p accumulates text fragment pairs in an array. It inserts a comma before all
    // except the first fragment pair.

    if (b) {
    a.push(',');
    }
    a.push(k.toJSONString(), ':', s);
    b = true;
    }

    // Iterate through all of the keys in the object, ignoring the proto chain.

    for (k in this) {
    if (this.hasOwnProperty(k)) {
    v = this[k];
    switch (typeof v) {

    // Values without a JSON representation are ignored.

    case 'undefined':
    case 'function':
    case 'unknown':
    break;

    // Serialize a JavaScript object value. Ignore objects that lack the
    // toJSONString method. Due to a specification error in ECMAScript,
    // typeof null is 'object', so watch out for that case.

    case 'object':
    if (this !== window)
    {
    if (v) {
    if (typeof v.toJSONString === 'function') {
    p(v.toJSONString());
    }
    } else {
    p("null");
    }
    }
    break;
    default:
    p(v.toJSONString());
    }
    }
    }

    // Join all of the fragments together and return.

    a.push('}');
    return a.join('');
    } has no method 'test'
    66beta
        12
    66beta  
       2013-06-21 18:39:57 +08:00
    <input type="button" id="tgr" value="点老子" />
    <div id="show"></div>
    <script type="text/javascript">
    $('#tgr').click(function(){
    $.getJSON("data.js", function(data){
    $("#show").html(data.username+':'+data.age+'岁');
    $("#tgr").val('激活码已发送').attr({'disabled':'disabled'});
    });
    });
    </script>
    ==========================================
    data.js
    {"username":"\u5f20\u4e09","age":"100"}
    ==========================================
    data.php 也可以的
    <?php
    header('Content-type: text/json');
    $json = array('username'=>'张三', 'age'=>'100');
    echo json_encode($json);
    ?>
    66beta
        13
    66beta  
       2013-06-21 18:46:24 +08:00
    我是Sublime Text 2 ,默认utf-8编码,无dom
    skydiver
        14
    skydiver  
       2013-06-21 18:51:04 +08:00 via Android
    json的MIME是application/json,不是text好吧……
    skydiver
        15
    skydiver  
       2013-06-21 18:52:15 +08:00 via Android
    还有代码要存成utf8格式,看上面的代码应该是存成gbk了
    Part
        16
    Part  
       2013-06-22 11:46:22 +08:00
    我一段
    $("html, body").animate({ scrollTop: 0 }, '520');
    也异常诡异,IE系全部可以执行(包括可恶的IE6),chrome竟然失效。调试得想死了.. mayday!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2941 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:16 · PVG 23:16 · LAX 08:16 · JFK 11:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.