0

I have a button with the class "btn" and span with id "ajaxtest". When I click on the button I want to put text "test" in my span tag. And this to be done in ASP.NET MVC.

In the View I have the following ajax call:

<span id="ajaxtest"></span>
<input type="submit" class="btn" name="neverMind" value="Test BTN"/>

    <script>
    $(document).ready(function () {
        $(".btn").click(function () {
            $.ajax({
                method: "get",
                cache: false,
                url: "/MyController/MyAction",
                dataType: 'string',
                success: function (resp) {                       
                    $("#ajaxtest").html(resp);
                }
            });
        });
    });
    </script>

In MyController I have the following code:

    public string MyAction()
    {
        return "test";
    }

I know how Ajax works, and I know how MVC works. I know that maybe the error is because we are expecting something like this in the controller:

public ActionResult MyAction()
    {
        if (Request.IsAjaxRequest())
        {
              //do something here
        }
        //do something else here
    }

But actually that is my problem. I don't want to call some partial View with this call. I just want to return some string in my span and I'm wondering if this can be done without using additional partial views.

I want to use simple function that will only return the string.

1
  • Change $("#ajaxtest").html(resp); to $("#ajaxtest").text(resp);. Additionally, add a debugger; in the success function to test the data you're getting back from the server. Commented Jun 25, 2014 at 12:49

1 Answer 1

3

Change dataType: 'string' to dataType: 'text'

<script>
            $(document).ready(function () {
                $(".btn").click(function () {
                    $.ajax({
                        method: "get",
                        cache: false,
                        url: "/login/MyAction",
                        dataType: 'text',
                        success: function (resp) {
                            $("#ajaxtest").html(resp);
                        }
                    });
                });
            });
        </script>

I check it my local it will work for me

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

1 Comment

This was the answer I was about to post and is right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.