简体   繁体   English

如何在jsp中使用jquery ajax?

[英]how to use jquery ajax in jsp ?

i have written a small code which inclued 2 jsp pages and 我写了一个小代码,其中包括2个jsp页面,

1) test2.jsp --> i have made a dropdown in this page 1) test2.jsp >我在此页面中进行了dropdown

2) test3.jsp --> displays a welcome message when the person selects an option from the dropdown , he will migrate to this page without refreshing test2.jsp 2)当用户从下拉列表中选择一个选项时, test3.jsp >显示欢迎消息,他将迁移到此页面而无需刷新test2.jsp

for this i have used ajax jquery 为此,我使用了ajax jquery

when i click on the submit button nothing happens , please help me with this i have also put the org.json.jar file in the libraries 当我单击“提交”按钮时,没有任何反应,请帮我解决这个问题,我还将org.json.jar文件放到了库中

test2.jsp code

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//malsup.github.com/jquery.form.js"></script>
    <script>
       function check()
       {     
$.ajax({
 url: "test3.jsp",
 type: "GET",

success: function() {
    alert("succes");
},

error: function() {
    alert( "Sorry, there was a problem!" );
}
});           
       }           
    </script>

</head>
<body>
    <h1>Hello World!</h1>
    <br>
    <form method="post" action="test2.jsp">
        <select name="city">
            <option value="dropdown">select city</option>
               <option value="jal">Jalandhar</option>
               <option value="ggn">Gurgaon</option>
               <option value="noida">Noida</option>
               <option value="amrtsr">Amritsar</option>
               <option value="bombay">Bombay</option>
        </select>
        <input type="submit" value="Submit" onclick="check()">
    </form>

test3.jsp code

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>welcome</h1>
</body>
</html>

The function should return false to prevent form submittion 该函数应返回false以防止表单提交

<script>
   function check()    {     

    $.ajax({
     url: "test3.jsp",
     type: "GET",

     success: function() {
        alert("succes");
     },

     error: function() {
        alert( "Sorry, there was a problem!" );
     }
    }); 
    return false;          
   }           

</script>

It should be 它应该是

<input type="button" value="Submit" onclick="check()">

instead of 代替

<input type="submit" value="Submit" onclick="check()">

because submit input type call form's action that is test2.jsp . 因为submit输入类型调用表单的操作是test2.jsp

call your javascript function on change event of drop down 在下拉事件更改时调用您的javascript函数

onchange="check()"

and navigate through firebug and check your consol it will show you your whole ajax request 并浏览萤火虫并检查您的控制台,它将显示您的整个ajax请求

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

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