0

我无法从表单 Spring Boot API 接收数据并获取响应中传递和获取数据。我得不到任何回应。请检查并给我解决方案PLZ

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<h1>Temparature converter</h1>


<form>
  <label for="temp">Choose Type : </label>
  <select name="type" id="type">
    <option value="kelvin">kelvin</option>
    <option value="fahrenheit">fahrenheit</option>
    <option value="celsius">celsius</option>
  </select>
  <br><br>
   <label for="from">From Unit : </label>
  <select name="fromUnit" id="fromUnit">
   <option value="kelvin">kelvin</option>
    <option value="fahrenheit">fahrenheit</option>
    <option value="celsius">celsius</option>
  </select>
    <br><br>
    <input  id="data" name="data" placeholder="Your data" type="text" />
  <input type="submit" id="myForm" value="Submit">
</form>

<div class="append-to-me"></div>


<script>
$(document).ready(function(){
  $('#myForm').submit(function(event){
  alert("YES");
  
   //  event.preventDefault();
     var data = {type: $("#type").val(), fromUnit: $("#fromUnit").val(),data: $("#data").val()};
     alert("YES");

     //Ajax code should be here
     $.ajax({
         type: "GET",
         url: "localhost:8080/convert/",
         data: data,
         cache: false,
         success: function(response) {
         console.log(response);
            var title = response;
             $('div.append-to-me').append(title);
         },
 
     }); 
   });
   });


</script>


</body>
</html>


我的 API 工作正常.. API 没问题。所以我认为我的问题在于ajax调用。我无法解决这个问题

API 图像

API 代码图像

4

1 回答 1

1

由于您尚未共享 API 的代码,并且正如我在您的 API 图像中看到的那样,您正在使用JSON.stringify(data). 但 API 需要一个对象。

尝试

$.ajax({
     type: "GET",
     url: "localhost:8080/convert/",
     data: data,
     cache: false,
     success: function(response) {
     console.log(response);
        var title = response;
         $('div.append-to-me').append(title);
     },

 }); 
于 2021-09-18T12:23:15.610 回答