axios 如何将JSON对象从Svelte传递到Quarkus

kmb7vmvb  于 2023-03-12  发布在  iOS
关注(0)|答案(1)|浏览(139)

我在前端使用苗条,在后端使用** quarkus
我发现自己无法获取从前端传递到后端的
search**方法的对象。
感谢您的帮助。

let endpoint = "http://localhost:8080/api/users/search";
   
   async function apiCall() {
      try {
        const res = await axios.post(endpoint, {
           headers: {},
           params: {name: 'John', surname: 'Doe'}
        });

        //...
      } catch (e) {
      }
   }

@Path("/api/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {
    
    //...
    
    @POST
    @Path("/search")
    public Response search(User user) {
        String name = user.getName();
        //This throws a NullPointerException because user is null
        
        //...   
    }
    
}
mspsb9vt

mspsb9vt1#

问题解决了。简单地说:

let data = {name: 'John', surname: 'Doe'};
const res = await axios.post(endpoint, data)

相关问题