JQuery的Ajax调用给出404“资源未找到”的错误,但正常的URL调用是精细 [英] JQuery Ajax call gives 404 'Resource Not Found' Error but Normal URL Call is Fine

查看:566
本文介绍了JQuery的Ajax调用给出404“资源未找到”的错误,但正常的URL调用是精细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC项目中使用JQuery电话时有一个奇怪的问题。我发现Ajax调用给404(未找到资源错误)。但是,当我使用普通的URL GET电话,我可以成功调用服务器没有任何问题。任何想法,为什么会这样?

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?

这是我的ASP.NET MVC code

This my ASP.NET MVC code

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

这是我的JQuery code:

This is my JQuery code:

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

以上的JQuery给了我一个404错误。显然, ViewRecord / GetSoftwareChoice 不是在服务器上找到,至于AJAX调用而言。

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.

但是,如果我在Web浏览器中键入以下内容:

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
    );

}

编辑:我踏进我的code和发现URL调用是 ViewRecord / GetSoftwareChoice用户名= 123 ?。

相关问题:<一href=\"http://stackoverflow.com/questions/1000246/select-element-inside-form-not-working-in-jquery\">Select里面的表单元素在JQuery中不工作

推荐答案

我用萤火给我看,是由JQuery的生成请求解决这个问题。令我吃惊的是,生成的URL是

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

对于JSON调用:

for the JSON call:

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

所以,我只需要在 $。的getJSON 行更改为

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

另外,使用正斜杠:

Alternatively, use the forward slash:

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

这篇关于JQuery的Ajax调用给出404“资源未找到”的错误,但正常的URL调用是精细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆