weixin_33735077 2014-07-16 12:50 采纳率: 0%
浏览 126

JSP不调用Ajax函数

I have written an ajax function which will be called when someone selects a year from a dropdown. On selecting the year, the ajax will call a servlet based on passed URL and that servlet will set a value in properties file. However, the problem is, on selecting the year, my ajax block is not called

</tr>
                    <tr>
                    <td>Year</td>
                    <td>
                        <html:select property="yearId" >
                            <html:options collection=
                                    "<%=GlobalValues.LIST_MODELYEAR%>" 
                                    property="id" labelProperty="value" />
                        </html:select>
                        (Required)
                    </td>
                </tr>

                <script>
        $(document).ready(function() 
        {
            $("#yearId").change(function() 
            {

                var selectedValue = $(this).find(":selected").val();
                $.ajax

                ({
                    url : "/ModelByYear.do?cID="+selectedValue+'',



                });
            });
        });
        </script>   
  • 写回答

2条回答 默认 最新

  • weixin_33747129 2014-07-16 13:10
    关注

    Remove the / from your URL as given below and the , is not neccesory

    $.ajax({
             url : "ModelByYear.do?cID="+selectedValue
          });
    

    Try by using the below code

    $.ajax({
      type: "GET",
      url: "ModelByYear.do",
      data: {cID:selectedValue},
      success: function(result){
                  alert('Result: ' + result);
               },
       error: function(err){
                   alert('Error: ' + e);     
              }
         });
    
    • type is html request type you can use GET or POST
    • ModelByYear.do is URL in you case you must map this url-pattern in WEB.xml

    While working on JSP's don't call jsp pages directly instead configure in WEB.xml as given here

    评论

报告相同问题?