0

I'm trying to make an Ajax call to my Spring MVC app using a Jquery Ajax. The app controllers are working fine but I can't get this Ajax test controller to work. The alert is triggered but the call to the controller is never made. I've also tried using load, get, post. None of them call the server. Which makes me think I'm doing something obviously wrong. If I put the URL directly on the browser address bar, the controller is called.

If someone can guide me in the right direction or tell me what I'm doing wrong, I'd be grateful.

JavaScript

<html>
<head>
<script type="text/javascript" src="jquery-1.8.1.min.js"> </script>
<script type="text/javascript">
    function doAjax() {
        alert("did it even get here?");
        $.ajax({
            url : "simpleRequestTest.do",
            method: "GET",          
            success : function(response) {
                $('#show').html(response);
            }
        });
    }
</script>
</head>
<body>
    <h1>Simple Test </h1>
    <a href="javascript:doAjax();"> Simple Test </a>
    <br />
    <div id="show">...</div>
</body>
</html>

Controller

@RequestMapping("/simpleRequestTest")
public @ResponseBody String performSimple()  {
    return "Very Simple Test";
}   
5
  • 3
    Hint: use the dev tools in your browser to watch the network traffic. You can see exactly what URL got hit, and what the server returned. This will most likely give you the information you need. Also, have you tried simply pointing your browser to simpleRequestTest.do? What happens? Commented Feb 20, 2013 at 5:25
  • You are requesting simpleRequestTest.do but your controller mapping is /simpleRequestTest without .do. I think the server is throwing a 404 not found error Commented Feb 20, 2013 at 5:33
  • I think your ajax url is not correct. Commented Feb 20, 2013 at 5:44
  • I spent all night on this so I was really looking forward to start trying out everyone's suggestions. So I boot up my PC, start eclipse, start tomcat & run my app.....and it works. I didn't change anything. It just works. I saw a similar question stackoverflow.com/questions/13047573/spring-mvc-ajax-jquery & the same thing happened to him. I really wish I had an answer in case the next person with the same issue is looking for a solution. In my case, rebooting seems to have worked. Thanks for the suggestions everyone. Commented Feb 20, 2013 at 12:47
  • @StriplingWarrior "have you tried simply pointing your browser to simpleRequestTest.do" That was one of the first things I tried. It worked fine, my debugger picked it up and then returned the String back to the browser. But obviously in a non ajaxy way. But when the url request went through the Ajax code, the server never got called. That is, until this morning when it started working. Commented Feb 20, 2013 at 14:46

3 Answers 3

1

Can you check whether you use correct path for include the jquery-1.8.1.min.js.

I checked the code it's working file for me.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you are missing dataType in ajax call

try this

function doAjax() {
        alert("did it even get here?");
        $.ajax({
            url : "simpleRequestTest.do",
            method: "GET",          
            success : function(response) {
                $('#show').html(response);
            },
            dataType: 'text'
        });
    }

Comments

1

Just change url : "simpleRequestTest.do", to url : "simpleRequestTest", and method: "GET", to type: "GET",

I think the method is removed in the jquery version 1.5 and latest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.