Ajax响应在使用jQuery 1.8.3时没有调用success:function()
,但代码与jQuery 1.4.2完美配合。
这里是代码:
<script type="text/javascript">
$(document).ready(function(){
$("#usn").on('keyup',function() {
var dat=$("#usn").val();
if($.trim(dat).length<10){
$("#result").html("");
$("#info").show();
} else if($.trim(dat).length==10) {
txt=$("#usn").val();
txt=txt.toUpperCase();
var exp = /^[1-9][a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z][a-zA-Z][0-9][0-9][0-9]$/;
if(exp.test(txt)){
var resultType =$("input[name='resultType']:checked").val();
$("#result").html("<img src=\"./result/loader.gif\"/><br/>Loading...");
$.ajax({
type: "GET",
url: "result/result_html.php?usn="+txt+"&resultType="+resultType,
dataType:"JSON",
success:function(result){
$("#info").hide();
$("#result").html(result);
$("#usn").attr("placeholder", "USN");
}
});
} else {
$("#result").html("Please enter a valid USN.");
}
}
});
});
</script>
控制台未显示任何错误。我可以在控制台中看到Ajax请求已成功完成。我可以在脚本控制台中看到预期的Ajax响应。
可能是什么问题?
答案 0 :(得分:4)
在$.ajax()
来电中,dataType
是JSON。您说您的评论中的回复是HTML。只需将dataType
更改为“html”就可以解决问题。
答案 1 :(得分:0)
作为您的发送回复html
...数据类型应为html
,在您的情况下为json
..纠正应该有效
$.ajax({
type: "GET",
url: "result/result_html.php?usn="+txt+"&resultType="+resultType,
dataType:"html", //<-- here
success:function(result){
$("#info").hide();
$("#result").html(result);
$("#usn").attr("placeholder", "USN");
}
}).....