简体   繁体   中英

A 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 does not work properly, so if i want add, delete, update a product or retrieve all products. it simples does not react to my clicks. I do not know what I have missed on my webpage, could you help me to fix that ?

From my webpage:

<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); }

It looks like you're trying to access your database (port 3306) instead of the Spring Boot application (that runs by default at port 8080).

Changing AJAX url to: http://127.0.0.1:8080/app/products should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM