Eric J.
Eric J.

Reputation: 150148

jQuery Success Callback Not Invoked

I have a link on an HTML page that is intended to refresh a div via Ajax.

The HTTP GET succeeds. I hit a breakpoint in the controller (it's an ASP.Net MVC 3 app), and I can see in the IE9 Network Profiler that the call happens when I click the link, returns an HTTP status 200, and the Response Body is exactly as I would expect.

However, my jQuery success method is not invoked.

Here's the code:

<script type="text/javascript">
    $(document).ready(function () {
        $('#add').click(function (e) {
            alert('I DO display');
            $.get('/Home/MyController?id=1&add=True&sub=3'), function (data) {
                alert('I do not display');
                $('#analysis').html(data);
            };
            e.preventDefault();
        });
    });
</script>  

What am I missing?

Upvotes: 1

Views: 78

Answers (1)

JohnColvin
JohnColvin

Reputation: 2509

It looks like it's just a syntax issue. Try this:

<script type="text/javascript">
    $(document).ready(function () {
        $('#add').click(function (e) {
            alert('I DO display');
            $.get('/Home/MyController?id=1&add=True&sub=3', function (data) {
                alert('I display now!');
                $('#analysis').html(data);
            });
            e.preventDefault();
        });
    });
</script>

The function was not being sent as a parameter because you closed the get call's parentheses too early. Javascript then doesn't complain that you define an anonymous function for no reason.

Upvotes: 1

Related Questions