Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have watched the video a couple times. For some reason, I just cannot figure out how to make a click button that says "Hello Ajax" with Ajax.

Here is my view for /Home/Index:

Razor
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>

    <script src="~/Scripts/jquery-1.10.2.js"></script>

    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

    <script>

        $(document).ready(function () {
            $("button").click(function () {

                $ajax({

                    type: 'GET',
                    url: '@Url.Action("HelloAjax" , "Home")',
                    success: function (result) {

                        ('#div1').html(result);

                    }

                });  // closes the ajax


            });  // closes click function
        });       // closes document ready function
    </script>




    <title>A study of population dynamics</title>


</head>
<body>


    <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

    <button>Get External Content</button>

</body>
</html>


What I have tried:

AJAX in ASP.NET MVC - YouTube[^]
Posted
Updated 12-Nov-16 4:10am

1 solution

You're missing a . - it's $.ajax, not $ajax:
jQuery.ajax() | jQuery API Documentation[^]

You're also missing a $ in your "success" callback.
JavaScript
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            type: 'GET',
            url: '@Url.Action("HelloAjax", "Home")',
            success: function(result){
                $("#div1").html(result);
            }
        });
    });
});


If that doesn't fix it, then you'll need to tell us what the error is. Open your browser's developer tools by pressing F12, and check the console for errors. Most browsers will also give you the option to examine any network requests made from the page, which will let you see the AJAX request and response, in case there's a server error.
 
Share this answer
 
Comments
forte74 12-Nov-16 15:04pm    
I am getting reference error : Ajax is not defined
Richard Deeming 12-Nov-16 17:41pm    
Then you need to check that your jQuery reference is loading properly.
forte74 20-Nov-16 23:30pm    
I fixed the 2 bugs and the click button works now , but I am not able to see anything with the debugger. I type in: result and get result
ReferenceError: result is not defined [Learn More]
Richard Deeming 21-Nov-16 8:33am    
result is a parameter passed to the success callback. It is not a global variable, so you can't just type the name into the console and expect to see the value.

Add a console.log(result); line to your success callback, or set a breakpoint on the first line within the body of the callback.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900