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

2013-06-21 14:16:35 +08:00
 xmbaozi
$('#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最蛋疼的,找不到错误的地方,有没有什么调试工具,或者方法。求推荐。
5031 次点击
所在节点    问与答
16 条回复
66beta
2013-06-21 14:26:00 +08:00
在$("#mobi_veri_code").val('a');前面加一句alert试试

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

控制台没给出信息吗?
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
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
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
2013-06-21 18:46:24 +08:00
我是Sublime Text 2 ,默认utf-8编码,无dom
skydiver
2013-06-21 18:51:04 +08:00
json的MIME是application/json,不是text好吧……
skydiver
2013-06-21 18:52:15 +08:00
还有代码要存成utf8格式,看上面的代码应该是存成gbk了
Part
2013-06-22 11:46:22 +08:00
我一段
$("html, body").animate({ scrollTop: 0 }, '520');
也异常诡异,IE系全部可以执行(包括可恶的IE6),chrome竟然失效。调试得想死了.. mayday!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/73135

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX