Ajax Servlet问题

时间:2015-05-26 19:55:36

标签: javascript ajax jsp

这里的问题是,如果我进行搜索,结果将在流程完成后不久返回,页面将使用json原始数据更改为servlet。 我在这里很困惑。

此JSP提交表单

Process: chinchillacongress.hangman, PID: 331
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4083)
        at android.view.View.performClick(View.java:4832)
        at android.view.View$PerformClick.run(View.java:19839)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4078)
            at android.view.View.performClick(View.java:4832)
            at android.view.View$PerformClick.run(View.java:19839)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5317)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
 Caused by: java.lang.IllegalArgumentException: n <= 0: 0
        at java.util.Random.nextInt(Random.java:182)
        at chinchillacongress.hangman.MainActivity.onStartButtonTouch(MainActivity.java:96)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4078)
            at android.view.View.performClick(View.java:4832)
            at android.view.View$PerformClick.run(View.java:19839)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5317)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

这是Servlet的目标

<form class="col-lg-12" action="./urllinks" method="GET" id="searchform">

                        <div class="input-group"
                            style="width: 340px; text-align: center; margin: 0 auto;">
                            <input class="form-control input-lg" title="Make a wish !."
                                placeholder="Go on and search ! Don't be shy :p" type="text" id="box"
                                name="query"> <span class="input-group-btn"><button
                                    class="btn btn-lg btn-primary" id="searchresult" type="submit">Search</button></span>
                        </div>

</form>

这是Javascript AJAX

    String word = request.getParameter("query");
    List<Page> results = DynamoDbMethods.search(word);

    String reply = new Gson().toJson(results); 

    response.setContentType("text/json"); 
    response.setHeader("Cache-Control", "no-cache");
    response.getWriter().write(reply);

我试图消除:$('#searchform').submit(function(){ var query = $("#box").val(); // alert(query); $.ajax({ target:"#map", dataType: "json", type: 'GET', data:{query: query}, url: 'http://localhost:8080/path/urllinks', success: function(response) { successCallback(response);} }); }); function successCallback(responseObj){ #This function just prints the data on a table #e.g $.each(responseObj, function(index, element){ alert(element.title);} } 但是没有数据

1 个答案:

答案 0 :(得分:0)

看起来你没有阻止默认的非ajax表单提交触发。

您的函数可以使用事件参数,您可以使用它来防止默认值。

$(document).ready(
    function () {   
        $('#searchform').submit(   
            function(event) {
                event.preventDefault();
                //rest of code
            }
        );
    }
); 

下面实际上有一个Jquery插件,您可以将所有样板减少到几行js。

http://jquery.malsup.com/form/