简体   繁体   English

jQuery AJAX完成未执行

[英]JQuery AJAX Done is not executing

$('#loginForm').submit(function(e){

        var $inputs = $(this).find("input");
        var serializedData = $(this).serialize();   
        $inputs.prop("disabled", true);

        var request = $.ajax({
            url: "myurl",
            dataType: "json",
            type: "GET",
            data: serializedData,
        });

        request.done(function (response, textStatus, jqXHR){
            $('#loginMessage').text('GOOD');
        });

        request.fail(function (jqXHR, textStatus, errorThrown){
            $('#loginMessage').text('Some error occured. Please try again');
            console.error("The following error occured: ",errorThrown,jqXHR);
        });

        request.always(function () {
            $inputs.prop("disabled", false);
        });
        // prevent default posting of form
        e.preventDefault();         
    });

I am new to jquery and in the above code .done block is not executing and firebug console display this message :- 我是jquery新手,在上面的代码中.done块未执行,firebug控制台显示此消息:-

GET myurl?userID=aman&password=aman 200 OK 37ms jquery....min.js (line 2) GET myurl?userID=aman&password=aman 200 OK 37ms jquery .... min.js(第2行)

The following error occured: (an empty string) Object { readyState=0, status=0, statusText="error"} 发生以下错误:(一个空字符串) Object { readyState=0, status=0, statusText="error"}

Server Side Scripting 服务器端脚本

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("userID");
        String password = request.getParameter("password");
        System.out.println("GET");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        Gson gson = new Gson();
        if(username.equals("aman") && password.equals("aman")){
            out.println(gson.toJson(new Boolean("true")));
        }else{
            out.println(gson.toJson(new Boolean("false"))); 
        }
        out.close();
    }

The issue likely is you aren't sending complete JSON. 问题可能出在您没有发送完整的JSON。 I output the result from 我从输出结果

out.println(gson.toJson(new Boolean("true")));

and all you get is the word true. 而您所得到的就是真实的词。 Try changing it to something like this. 尝试将其更改为此类。

HashMap<String, Boolean> hm = new HashMap<String, Boolean>();
hm.put("success", true);
out.write(gson.toJson(hm));

Running that I got {"success":true} which is valid JSON. 运行我得到{“ success”:true}这是有效的JSON。

Probably done is not called because fail is called instead. 因为调用了fail,所以未调用完成的事件。 Since the http request itself seems to have worked, another possible problem is that it did not return proper json content, whereas the reply is interpreted as such (because of dataType: "json"). 因为http请求本身似乎已经起作用,所以另一个可能的问题是它没有返回正确的json内容,而答复被解释为这样(由于dataType:“ json”)。

So, you should investigate about what content the server is returning. 因此,您应该调查服务器返回的内容。

I don't know why, but sometimes firebug breakpoints give the impression that the code in request.done() is not executed. 我不知道为什么,但是有时萤火虫断点给人的印象是request.done()中的代码未执行。 Maybe it is simply channeled to the console. 也许只是将其引导到控制台。

Try testing your code without breakpoints in firebug. 尝试在Firebug中测试没有断点的代码。

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

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