Question

I've looked at several solutions for making an ajax call and by not this issue mentioned anywhere i feel it might be something specific to the environment i'm working with. My controller:

    [HttpPost]
    public ActionResult ChangeDefualtCC(string a)
    {

        return Json("ok");
    }
    [HttpGet]
    public ActionResult ChangeDefualtCC()
    {

        return Json("ok");
    }

JS:

    $("nevermind").change(function () {

    $.ajax({
        type: "POST",
        url: "/Account/ChangeDefualtCC",
        dataType: "json",
        data: {
            a: "A"
        },
        success: function (data) { console.log(data)},
        error: function (data) { console.log("error");}
    });
});

The Controller code is never hit, and this is what i'm seeing in chrome after the ajax call:

EDIT 2: The page hits the [HttpGet] method.

chrome network tab

EDIT: I tagged Ektron as well because it is used in the project, and it is possible that it is affecting the call.

My Routes: mvc routes

Update: I have tried using Get, as well as Post, and also returning back to the View I was in, I get the 302 everytime.

any ideas?

Was it helpful?

Solution 3

Our project is integrated with the CMS Ektron. We later discovered that Ektron is hit before the C# code, and has some affect to any url without a trailing url.

Thanks for all the help

OTHER TIPS

It looks like it finds the "get" because you don't have a parameter in that call. I think you might be missing the content type from your ajax call, so the model binder cannot parse the content of your post as a parameter.

$.ajax({
        type: "POST",
        url: "/Account/ChangeDefualtCC",
        contentType: 'application/json; charset=utf-8',       
        dataType: "json",
        data: {
            a: "A"
        },
        success: function (data) { console.log(data)},
        error: function (data) { console.log("error");}
    });

Your code seems to be absolutely correct. This not be exact solution but try this may it work.

 $("nevermind").change(function () {

     $.post("/../Home/ChangeDefualtCC", { a: "A" }, function (data) {

        console.log(data)
     });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top