1

I can not get to the controller method. I think it may be the url, but I have tried different variations of it. I have been trying to figure this out for way too long.

$.ajax({
                    url: '@Url.Action("ajaxCall", "ContactForm")',                          
                    type: "POST",
                    cache: false,
                    dataType: 'json',                       
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify("test"),           
                    success: function () {
                        alert("success");
                    },
                    error: function () {
                        alert("fail");
                    }

                })

Here is my controller.

    namespace form.Controllers
    {
        public class ContactFormController : Controller
        {


            [HttpPost]
            public ActionResult ajaxCall(string s)
            {            
                return Content("");
            }
    }
}

Any idea what is wrong?

0

5 Answers 5

2

I see 2 issues with the example you provided.

  1. The URL.Action method will return only '/ContactForm/ajaxCall' when what you need is the fully qualified URL ('http://servername.com/ContactForm/ajaxCall')

  2. The client javascript is expecting the server to send back JSON and your controller action is returning HTML. Change the controller action to return 'JsonResult' and it should work fine, I tested.

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

Comments

0

Don't specify the word "Controller" in the call to @Url.Action():

@Url.Action("ajaxCall", "ContactForm")

It's not looking for the class name, it's looking for the controller name, and in ASP.NET MVC convention the word "Controller" is part of the class name to indicate that it's a controller otherwise named "ContactForm" in this case.

3 Comments

@mwhite14: What's the response from the server?
It is never makes it to the server. It is going to the error function. The data value it gives it [object Object], not that it matters since it is not even getting to the server.
@mwhite14: What's the error that causes it to go to the error condition? Are you sure it's not getting some response from the server? If it's not even sending a request to the server then something must be wrong client-side. Is there an error on the JavaScript console? Is there a network request at all? How are you confirming that it's not going to the server?
0

If David's answer doesn't get you all the way there (definitely look at the rendered page to see what it outputs to be sure), try wrapping the Razor code in braces:

@{Url.Action("ajaxCall", "ContactForm")}

Comments

0

Have you tried:

url: '@Url.Action("ajaxCall", "ContactForm")

You shouldn't be specifying the full name of the controller class. You can also try the route name.

1 Comment

@mwhite14: Hard code the URL to eliminate that possibility. Then try to figure out what the specific error is. code error: function (error) { alert('error; ' + eval(error)); } code
0

Try posting a javascript object like this:

var value = { s : 'Test string' }

And on the ajax call:

data: JSON.stringify(value)

So the model binder will find an 's' property to map to the 's' parameter.

According to the dataType ajax option, you should return a JsonResult from the action: this option is the expected type of response received from the server.

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.