xmbaozi
V2EX  ›  问与答

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

By xmbaozi at 2013 年 6 月 21 日 · 6028 次点击
$('#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 年 6 月 21 日   ❤️ 1
在$("#mobi_veri_code").val('a');前面加一句alert试试

不好意思,俺就是喜欢那么做~~
xmbaozi
   2
xmbaozi  
OP
   2013 年 6 月 21 日
嗯。。我也喜欢那么干。
一样没反应,没报错。IE8 可以弹出
xmbaozi
   3
xmbaozi  
OP
   2013 年 6 月 21 日
@66beta ↑↑ 补@ 谢谢
revlis7
   4
revlis7  
   2013 年 6 月 21 日   ❤️ 1
看看返回的json数据有没有问题?如果getJSON返回数据格式有问题,好像不会触发callback。
xmbaozi
   5
xmbaozi  
OP
   2013 年 6 月 21 日
@revlis7 返回
{"code":0}
没错吧 状态是200
juicy
   6
juicy  
   2013 年 6 月 21 日   ❤️ 1
@xmbaozi 返回数据格式不对的可能性比较大, 也许是编码问题, 返回的数据是utf-8 withou BOM的格式么? 如果带有BOM,可能无法正确解析
csx163
   7
csx163  
   2013 年 6 月 21 日
调试工具:chrome 按F12 点console
我一般是用console.log()来调试,很方便
binux
   8
binux  
   2013 年 6 月 21 日
Content-Type
yleo77
   9
yleo77  
   2013 年 6 月 21 日 via iPhone
…
控制台没给出信息吗?
ijse
   10
ijse  
   2013 年 6 月 21 日
@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 年 6 月 21 日
后端返回数据时应设成`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 年 6 月 21 日
<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 年 6 月 21 日
我是Sublime Text 2 ,默认utf-8编码,无dom
skydiver
   14
skydiver  
   2013 年 6 月 21 日 via Android
json的MIME是application/json,不是text好吧……
skydiver
   15
skydiver  
   2013 年 6 月 21 日 via Android
还有代码要存成utf8格式,看上面的代码应该是存成gbk了
Part
   16
Part  
   2013 年 6 月 22 日
我一段
$("html, body").animate({ scrollTop: 0 }, '520');
也异常诡异,IE系全部可以执行(包括可恶的IE6),chrome竟然失效。调试得想死了.. mayday!
© 2026 V2EX · 40ms · 3.9.8.5