0

I'm beginner in asp.net mvc ,i want to call the controller action from Ajax code ,for that purpose read this tutorial:
CALL CONTROLLER WITH AJAX
write this action method in my controller:

[HttpPost]
        public void Test01()
        {
            string behzad = "BEHZAD RAZZAQI";

        }


and in view page write this html code:

<button type="button" id="btn1" class="btn btn-success">ثبـت نـام</button>


in on that view page write this jquery code:

<script>
        $("#btn1").click(function () {
            $.ajax({
                url: "/MainPage/Test01",
            datatype: "text",
            type: "POST",
            success: function (data) {
                alert('ok');//$('#testarea').html("All OK");
            },
            error: function () {
                $("#testarea").html("ERROR");
            }
            });
        });
    </script>


but when i fire the button i can not see any alert,what happen?how can i solve that problem?

3
  • 1. Is 'MainPage' given in ajax url your controller name? 2. Is that javascript method hitting when you click the button? Try putting an alert before ajax call. 3. Is there any error in the browser console? Commented Aug 21, 2016 at 17:38
  • any error you seen in Browser Console ? Commented Aug 21, 2016 at 18:10
  • @Developer i get this message in browser console:Warning: NetUtil.asyncFetch() requires the channel to have one of the security flags set in the loadinfo (see nsILoadInfo). Please create channel using NetUtil.newChannel() Commented Aug 21, 2016 at 18:21

2 Answers 2

2

Try this instead:

 $(document).ready(function() {

    $("#btn1").click(function () {
        $.ajax({
            url:'<%= Url.Action("Test01", "MainPage") %>',
            success: function(data) {
                 alert("ok");
            }
        });
    });

 });

Make sure that MainPage is the name of your controller and that you included your jquery library, just like @bhupesh stated:

<script src="~/scripts/jquery-*.*.*.min.js"></script>

Further information can be found Here

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

5 Comments

i want that ajax run when the button fire,How can change that code?
Just put that code inside your button click funcion.
i write simle test function into script tag,and write onclick="test()" into the button click ,into test function write alert('ok') and when button fire that show,but when your ajax file copy into test function and fire the button,alert not show me
thanks my friend,write simple alert into the $("#btn1").click(function () { function,out of ajax,and dont show that!
mmm... It migth be due to your document is not ready or might be due to you are not including your jquery library properly... please, see your page source code and make sure you are referencing the jquery library properly.. anyway I updated the answer for executing the code just after your document is ready..
0

please make sure you have included jquery script file in your page.

<script src="~/scripts/jquery-*.*.*.min.js"></script>

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.