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>   

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

Maybe you can check the url you build first before calling the AJAX?

$(document).ready(function() 
        {
            $("#yearId").change(function() 
            {
                var selectedValue = $(this).find(":selected").val();
                window.location = "/ModelByYear.do?cID="+selectedValue;
            });
        });