RESTful web service using MySQL Community Server and jQuery AJAX

I am trying to create a RESTful web service using MySQL Community Server and jQuery AJAX

My jQuery AJAX is not working correctly, so if I want to add, remove, update a product or get all products. it just doesn't respond to my clicks. I don't know what I missed on my webpage, can you help me fix this?

On my web page:

<button onclick="addProduct()"> Save </button>
<script>
    function addProduct() {
        var productData = {
            id: document.getElementById("id").value,
            name: document.getElementById("name").value,

        }

        $.ajax({
            url: "http://127.0.0.1:3306/app/products",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: "POST",
            dataType: "json",
            data: JSON.stringify(productData)
        });
    }
</script>

      

From my java class:

@RequestMapping(method = RequestMethod.GET, value = "/app/products")
    public List<Product> getAllProducts(){
        return productService.getAllProducts();}

@RequestMapping(method = RequestMethod.POST, value = "/app/products")
        public void addProduct(@RequestBody Product product){
            productService.addProduct(product); }

      

+3


source to share


1 answer


It looks like you are trying to access your database (port 3306) instead of your Spring Boot application (which runs on port 8080 by default).



Change AJAX URL: http://127.0.0.1:8080/app/products

Should work.

0


source







All Articles