ajax请求404not found,JQuery的Ajax调用给出404“资源未找到”的错误,但正常的URL调用是精细...

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.

解决方案

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'},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值