views:

1037

answers:

4

I have a weird problem when using JQuery call in my ASP.NET MVC project. I found that the Ajax call gives 404 ( resource not found error). But when I use the usual URL GET call, I can successfully call the server without any problem. Any idea why this is so?

This my ASP.NET MVC code

public class ViewRecordController: Controller
{
  public JSONResult GetSoftwareChoice(string username)
  {
     return Json(username);
  }
}

This is my JQuery code:

$(function() {
$("#username").click(function() {
        $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
    function(data) {
        alert(data);
    });
    });
});

The above JQuery gives me a 404 error. Apparently the ViewRecord/GetSoftwareChoice is not found on server, as far as the AJAX call is concerned.

But if I type this in my web browser:

http://myapp/ViewRecord/GetSoftwareChoice?username=123

then there is no problem.

This is very weird, indeed.

Just in case if you are interested, this is my route:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

}

Edit: I step into my code, and found that the URL call is ViewRecord/GetSoftwareChoice?username=123.

Related question: Select Element inside Form not working in JQuery

+4  A: 

Instead of hard-coding the URL, you might want to try a UrlHelper:

$(function() {
    $("#username").click(function() {
        var url = '<%= UrlHelper.Action("GetSoftwareChoice", "ViewRecord") %>';
        $.getJSON(url, {username: '123'}, function(data) {
            alert(data);
        });
    });
});
Johnny G
Exactly - I'm thinking that it's also an issue with the jQuery fetch url being relative to the current path, instead of from the app root.
Jarrod Dixon
Also, +1 for using FireBug and showing us the FireBug console results.
Pure.Krome
I don't think UrlHelper works in Javascript ( I have tested it)... are you sure it works?
Ngu Soon Hui
It wouldn't work in a separate .js file, but it would work inside a <script> tag in an aspx/ascx file.
Johnny G
Gosh... that's too bad a limitation. I always keep my javascript in a separate .js file, for easier reading..
Ngu Soon Hui
If you keep it in a separate file then you need to take notice of what Johnny G was REALLY saying and what Jarrod pointed out - the url you have ("ViewRecord/GetSoftwareChoice") is a relative path and probably not what you want - you need to change it to the correct relative path or make it an absolute path.
Charlino
A: 

Use Firefox Firebug add on, and watch what request gets made by Jquery...

Is it possible that the page that this Jquery runs in is in a subdirectory, in which case the request will not be relative root as the http://myapp/ "typed in" url is?

Also, I am guessing the code you specified above is not actually the code you are using (which is completely reasonable, I rairly post code as-is). Because

$.getJSON("ViewRecord/GetSoftwareChoice", {username='123'},

the = sign between username and '123' is invalid JS as far as I know. So I'm betting there is some goofy detail in the the real code that is causing the problem.

larson4
I am using ASP.NET MVC, so there is no problem of request will not be relative root as the http://myapp/ "typed in" url is.
Ngu Soon Hui
A: 

Replace the equal sign with a colon:

$(function() {
$("#username").click(function() {
        $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
    function(data) {
        alert(data);
    });
    });
});
Marwan Aouida
Sorry! It's a typo in my question. I am using {username:'123'} in my code but accidentally typed {username='123'}
Ngu Soon Hui
A: 

I fix this problem by using FireBug to show me the request that was generated by JQuery. To my amazement, the url generated is

http://localhost/ViewRecord/ViewRecord/GetSoftwareChoice?username=123

for the JSON call:

$(function() {
$("#username").click(function() {
        $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
    function(data) {
        alert(data);
    });
    });
});

So I just have to change the $.getJSON line to

$.getJSON("GetSoftwareChoice", {username:'123'},

Alternatively, use the forward slash:

 $.getJSON("/ViewRecord/GetSoftwareChoice", {username:'123'},
Ngu Soon Hui
.... or to "/ViewRecord/GetSoftwareChoice". the leading slash means an absolute path, even if you don't include the hostname
Javier
Thanks, Javier. I included your response in my answer.
Ngu Soon Hui
I think Johhny G (and Jarrod in the comments) already implied this answer.
Charlino