AJAX Jquery MVC5 404错误


                    

我是MVC的新手,我对MVC / AJAX通话有疑问。</ p>

我正在尝试从MVC控制器取回数据,但它一直说404错误</ p>

 “找不到网络错误:404-http:// localhost:6481 / MDT / Detail?id = 4935”
</ code> </ pre>

我的Javascript调用如下,我将硬编码ID:1245作为测试。 我尝试切换POST和GET,但仍然没有运气</ p>

  $ .ajax({
     类型:“ GET”,
     网址:“ MDT /详细信息”,
     数据:JSON.stringify({
         id:1245
     }),
     contentType:“ application / json”,
     dataType:“ JSON”,
     成功:功能(数据){
         console.log(data);
     },
     失败:函数(数据){}
  });
</ code> </ pre>

我尝试了不使用JSON.stringify的情况,但错误仍然相同(404)</ p>

  $。ajax({
     类型:“ GET”,
     网址:“ MDT /详细信息”,
     数据:{
         id:1245
     },
     contentType:“ application / json”,
     dataType:“ JSON”,
     成功:功能(数据){
         console.log(data);
     },
     失败:函数(数据){}
});
</ code> </ pre>

以下是Controller中的代码。</ p>

  [Route(“ MDT / Detail / {id}”)]
     公共JsonResult详细信息(int?id)
     {
         ITS.Models.ComputerDetail cp = GetDataFromDatabase(id.Value);

         返回Json(cp,JsonRequestBehavior.AllowGet);
     }
</ code> </ pre>

我已经检查了Firebug,并按如下所示正确传递了参数:</ p>

我不知道自己做错了什么。 您能帮我解决这个错误吗?</ p>

如果我使用过( http:// localhost:6481 / MDT / Detail / 1245 ) ,它可以正常工作并返回JSON对象,但我无法从JQuery AJAX调用此MVC方法。</ p>
     </ div>

展开原文

原文

I am very new to MVC and I have got a question about MVC / AJAX Calls.

I am trying to get the data back from MVC Controller but it keeps saying 404 Error

"NetworkError: 404 Not Found - http://localhost:6481/MDT/Detail?id=4935"

My Javascript call is as below and I hardcoded id:1245 as the test. I tried to switch POST and GET, but still no luck

 $.ajax({
    type: "GET",
    url: 'MDT/Detail',
    data: JSON.stringify({
        id: 1245
    }),
    contentType: "application/json",
    dataType: "JSON",
    success: function(data) {
        console.log(data);
    },
    fail: function(data) {}
 }); 

I have tried it without JSON.stringify and the error is still the same (404)

$.ajax({
    type: "GET",
    url: 'MDT/Detail',
    data: {
        id: 1245
    },
    contentType: "application/json",
    dataType: "JSON",
    success: function(data) {
        console.log(data);
    },
    fail: function(data) {}
}); 

The following is the code in the Controller..

    [Route("MDT/Detail/{id}")]
    public JsonResult Detail(int? id)
    {
        ITS.Models.ComputerDetail cp = GetDataFromDatabase(id.Value);

        return Json(cp, JsonRequestBehavior.AllowGet);
    }

I have checked in Firebug and the parameters are passed correctly as following:

I couldn't figure out what I have done wrong. Could you please help me with this error?

If I used (http://localhost:6481/MDT/Detail/1245), it works fine and returns the JSON object but I couldn't call this MVC method from JQuery AJAX.

2个回答


您必须发送id,而不是像param一样,而是像url的一部分一样。 将您的代码更改为此:</ p>

  $。ajax({
     类型:“ GET”,
     网址:“ MDT / Detail / 1245”,
     contentType:“ application / json”,
     dataType:“ JSON”,
     成功:功能(数据){
         console.log(data);
     },
     失败:函数(数据){}
});
</ code> </ pre>
     </ div>

展开原文

原文

You must send id not like param but like part of url. Change your code to this:

$.ajax({
    type: "GET",
    url: 'MDT/Detail/1245',
    contentType: "application/json",
    dataType: "JSON",
    success: function(data) {
        console.log(data);
    },
    fail: function(data) {}
});


答案就是不使用</ p>

  JSON.stringify({id:1245})
</ code> </ pre>

只是通过</ p>

 数据:{id:1245}
</ code> </ pre>

与</ p>

在向控制器操作添加 [HttpPOST] </ strong>属性之后,POST请求也起作用。</ p>

根据您的ajax调用,MVC默认路由为{Controller} / {Action} / {id},路由为 http:// localhost:6481 / MDT / Detail?id = 4935 ,并且id被用作查询字符串参数,因此您需要将Route用作 http:// localhost:6481 / MDT / 493 。</ p>
     </ div>

展开原文

原文

Answer is just not to use

JSON.stringify({ id: 1245 })

just pass

data:{ id: 1245 }

with

Also POST request working after adding [HttpPOST] Attribute to Controller Action.

And MVC default routing is {Controller}/{Action}/{id} as per your ajax call Route is http://localhost:6481/MDT/Detail?id=4935 and id is taken as Query String Parameter so for that you need to use Route as http://localhost:6481/MDT/493.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐