0

On button click I am trying to send a product name value that the user enters into a textbox to the server to be modified and then back to the page using AJAX. I am getting into the ChangeName method in the controller but not getting into my success function to alert the new name.

The JS:

    $("#changeNameButton").click(function () {
        var productName = $("#Name").val();

        $.ajax({
            url: '/Products/ChangeName/',
            type: 'POST',
            dataType: 'JSON',
            data: {name: productName},
            success: successFunc
        });
    });


    function successFunc(data) {
        alert(data);
    }

The controller:

    public string ChangeName(string name)
    {
        string changedName = ChangeNameHelper(name);
        return changedName;
    }

If anyone can give recommendations on the proper way to make asynchronous calls to a controller in MVC5/6 this would be great.

My main problem is that I am never getting into the successFunc() on response.

18
  • 1
    You might wanna return name; rather than return ChangeName(name) as you're making recusive calls. Commented Jan 19, 2016 at 19:19
  • 1
    It looks like the ChangeName function just calls itself, which would result in an infinite loop. It never gets to success because the server would never return a response to the client. Commented Jan 19, 2016 at 19:20
  • @remdevtec I am now setting the new changed name to a variable then trying to return it, but the alert still isn't happening. Can you return just a string back to AJAX? Or do I have to return a JsonResult or ActionResult instead? Commented Jan 19, 2016 at 19:32
  • @BlakeRivell It looks like it's doing the same thing. By calling ChangeName(name) as the first line, you'll just keep calling the ChangeName function infinitely. You either need to call another function (to do some work) or just return name; Commented Jan 19, 2016 at 19:35
  • I apologize! in my actual code ChangeName is a helper function with a different name. Now look at my updated code. Commented Jan 19, 2016 at 19:37

3 Answers 3

1

Regarding your comment, if you return just a string MVC will convert that to json.

Now, in your updated code you still call a method inside itself. Please call string changedName = ChangeNameForResult(name); or any function with another name.

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

1 Comment

I think my problem is that I am never getting into my success function. Not sure what could be going wrong. I see in the debugger in the controller that it returns the changed name string.
0

Install Newtonsoft.Json via nuget package manager and in your controller do this

Public ActionResult ChangeName(string name)
{
     // get your object you want to serialize let's say res
     return JsonConvert.SerializeObject(res);

}

7 Comments

I think you have me on the right track but I am not dealing with objects in this case. Just a simple string that I want to return. Can res be a string? Intellisense is saying no.
return Content(YourString, "application/json");
Still not able to alert the response. I am getting a 200OK in the network tab which is strange.
Did you change your action returning ActionResult instead of string ??
Yes, but still not getting into the success function to alert the data result. And by the way is that something that is required when using jQuery AJAX?
|
0

I needed to set dataType: 'text' rather than JSON since I am returning a simple string.

dataType is the type of data being returned and contentType is the type of data being sent.

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.