Ashish Srivastava
Ashish Srivastava

Reputation: 47

Get Error on Ajax call Spring MVC Controller

I want get Data from Spring MVC using Ajax Request My Ajax Script is

$.ajax({
            url: "http://localhost:8080/statuteList",
            type: "GET",
            success: function (data, status) {
                alert(data);
            },
            error: function (xhr, desc, err) {
               alert("error "+err+" "+desc+" "+xhr.responseText);
            },
        });

and My Spring MVC Program is

@RequestMapping(value = "/statuteList",method = RequestMethod.GET)
    public @ResponseBody StatuteWrapper[] statuteList() {
        ArrayList <StatuteID> al = ReferencePool.getStatuteList();
        StatuteWrapper[] sWrapper = new StatuteWrapper[al.size()];

        System.out.println("Sending StatuteList");
        for (int i=0; i < al.size(); i++){
            sWrapper[i] = new StatuteWrapper(al.get(i));
        }

        return sWrapper;
    }

i am getting only in alert of error is "error error" only please help me

Upvotes: 0

Views: 1370

Answers (1)

Roman
Roman

Reputation: 323

You have to modify your rest controller and enable CORS. I know two ways to implement that.

One way is to add the annotation @CrossOrigin in your Controller's method, here you have the official documentation: https://spring.io/guides/gs/rest-service-cors/

Another way is to add a filter and modify the request header. You have a perfect example here: https://gist.github.com/zeroows/80bbe076d15cb8a4f0ad

Upvotes: 1

Related Questions