4

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?

edank
  • 599
  • 7
  • 16
  • 1
    You spelled "Default" wrong :) – Mansfield Jan 23 '14 at 18:47
  • I think something's missing here. What's the full URL for the initiator column in Chrome dev tools? – Ant P Jan 23 '14 at 18:48
  • I knew something didn't look quite right about that word.. And yes, the controller is AccountController.cs – edank Jan 23 '14 at 18:48
  • In general, if your endpoint isn't hit at all, you want to look at your routing. Post your routing code, and maybe we can help. – Steven Hansen Jan 23 '14 at 18:50
  • Ant P. what do you mean by initiator column? – edank Jan 23 '14 at 18:52
  • This project is also using Ektron, it's possible that it's own routing is affecting the call. Does anyone have experience with that? – edank Jan 23 '14 at 19:06
  • I mean in Chrome dev tools, the cell that says `http://localhos...` - what is the full URL? – Ant P Jan 23 '14 at 19:14
  • What happens if you remove the ajax call in that function and change "nevermind"? Is there a Get call in the console as well? – mart Jan 23 '14 at 19:17
  • Ektron is primarily web-forms based; you have to add your own routing if you want to try to use MVC with Ektron. – Brian Oliver Jan 23 '14 at 19:31
  • Ant P. the initiator for the Get call is - http://localhost:53555/Account/ChangeDefaultCC Redirect. – edank Jan 23 '14 at 19:39
  • Brian, the routing exists for .NET. I just don't know why that call would be redirected, for this page it using the standard routing of /controller/action. – edank Jan 23 '14 at 19:40
  • Can you post your routes as that's most possibly where the fault lies – Simon Halsey Jan 23 '14 at 23:07
  • Simon, I updated my question with the routes. – edank Jan 23 '14 at 23:24
  • I noticed there is no answer to this yet. Have you resolved the issue? If not, do you have Ektron's Aliasing function enabled? It uses a custom Module/Handler that might be interfering. – Daved Feb 20 '14 at 21:20

3 Answers3

0

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");}
    });
gabnaim
  • 1,103
  • 9
  • 13
  • gabanim, I added the contentType, but the Get is still the method being called – edank Jan 23 '14 at 20:23
  • Sorry that didn't help. I wonder though if your problem has something to do with the way you send the parameters. The GET method takes no parameters and it is found. Maybe if you changed them around - i.e., made the POST parameterless and the GET take the string parameter - you could narrow it down if it is a parameter issue or not. – gabnaim Jan 24 '14 at 17:43
  • I did try that yesterday as well, I tried many combinations of none, one and more parameters; it always went to the Get. But it never goes straight to get, it receives a 302 first. Same thing happened when i tried wrapping it with a form and submitting instead. – edank Jan 24 '14 at 18:58
0

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)
     });
});
Muntajib
  • 141
  • 5
0

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

edank
  • 599
  • 7
  • 16