简体   繁体   English

使用jQuery 1.8.3时Ajax响应未调用success:function()

[英]Ajax response not calling success:function() when using jQuery 1.8.3

Ajax response not calling success:function() when using jQuery 1.8.3 but the code is working perfectly with jQuery 1.4.2. 使用jQuery 1.8.3时,Ajax响应未调用success:function() ,但代码与jQuery 1.4.2完美配合。

Here the code: 这里的代码:

<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>

The console is not showing any error. 控制台未显示任何错误。 I can see in the console Ajax request is successfully completed. 我可以在控制台中看到Ajax请求已成功完成。 I can see the expected Ajax response in my script console. 我可以在脚本控制台中看到预期的Ajax响应。

What might be the problem? 可能是什么问题?

Within your $.ajax() call, the dataType is JSON. $.ajax()调用中, dataType为JSON。 You said the the response was HTML in your comments. 您说评论中的回复是HTML。 Simply changing the dataType to "html" should solve the issue. 只需将dataType更改为“ html”即可解决此问题。

As your sending response as html ...the datatype should be html which in your case is json .. correcting that should work 作为您的发送响应为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"); 
                }
        }).....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM