Inoka
Inoka

Reputation: 1

Ajax with Spring boot

Sorry for asking this question as a new Question. I changed the code as instructed. but still it's not working. Please help me to solve this coding. Thanks !

            <div class="label_left_2">
                <label class="outputdataField2_Short">Product No</label>
            </div>
            <div class="input_left_2">
                <input list="dropdownproduct" placeholder="Start typing..."
                    class="inputDataField_Select2_Short" id="dropProduct"
                    th:field="*{product_id}">
            </div>
            <datalist id="dropdownproduct">
                <option value="0">select product Code</option>
                <option th:each="product1 : ${listProducts}"
                    th:value="${product1.item_Code}" th:text="${product1.name}" />
            </datalist>
            

Ajax Code

  $( "#dropProduct" ).select(function() {
            //  alert( "Handler for .select() called." );
              var productId= $( "#dropProduct" ).val();
             // alert(' productId'+ productId);
              $ajax({
              url :'/findProduct2',
            
              **data:{ "productid" : productId },**


              dataType:'json',
              contentType:'application/json', 
              success: function(result){
              $(discount_Amount).html(result.discount);
              }
              //}
              });
              
              
            });

Spring Controller Code:-

@RequestMapping("/findProduct2")
    ResponseEntity<Product> showProduct2(HttpServletRequest req, Model model) {
        System.out.println(" in method.... ");
        **String productId = req.getParameter("productId");**
        ResponseEntity<Product> responseEntity = new ResponseEntity<Product>(productService.get(productId),
                HttpStatus.OK);
        return responseEntity;

    }

Upvotes: 0

Views: 922

Answers (1)

Jalil.Jarjanazy
Jalil.Jarjanazy

Reputation: 871

It's probably because you forgot to specify the HTTP method both in the Ajax and Spring controller. I am assuming it's a GET request.

try this for the Ajax

 $( "#dropProduct" ).select(function() {
            //  alert( "Handler for .select() called." );
              var productId= $( "#dropProduct" ).val();
             // alert(' productId'+ productId);
              $ajax({
              url :'/findProduct2',
            
              **data:{ "productid" : productId },**


              dataType:'json',
          type: "GET",
              contentType:'application/json', 
              success: function(result){
              $(discount_Amount).html(result.discount);
              }
              //}
              });
              
              
            });

And this for the Controller

@RequestMapping("/findProduct2", method = RequestMethod.GET)
    ResponseEntity<Product> showProduct2(HttpServletRequest req, Model model) {
        System.out.println(" in method.... ");
        **String productId = req.getParameter("productId");**
        ResponseEntity<Product> responseEntity = new ResponseEntity<Product>(productService.get(productId),
                HttpStatus.OK);
        return responseEntity;

    }

Upvotes: 1

Related Questions