2012-06-27 146 views
1

Chrome调试器显示名称为“GetPurchaseOrdersComponent”,路径显示为“/ Cost”,而Telerik网格控件的调用名称为“4485”,路径为“/ Cost/GetPurchaseOrders “(他们正在工作)。另外,我的调用中的类型(在Chrome调试器中查看时)是text/html,而在工作调用中,它是application/json。我收到的错误是:“500(内部服务器错误)”。对于其他呼叫,我为此呼叫定义了类似的路由。这里是我的代码:jquery ajax调用MVC控制器失败

$.ajax({ 
     url: "/Cost/GetPurchaseOrdersComponent", 
     type: "GET", 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     async: true, 
     data: { id: 1 }, 
     success: function (result) { 
      $("#ComponentsMultiLevelGrid").html(result); 
     } 
    }); 


[HttpGet] 
public string GetPurchaseOrdersComponent(int id) 
{ 
    return "some string"; 
} 

UPDATE:

下面是一个可行的通话头(此电话是从Telerik的网格):

Request URL:http://localhost:61751/Cost/GetSupplierCatalogs/4485?key=4485&_=1340830508447 
Request Method:POST 
Status Code:200 OK 

**Request Headers** - view source 
Accept:text/plain, */*; q=0.01 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:13 
Content-Type:application/x-www-form-urlencoded 
Cookie:ASP.NET_SessionId=uxn1ctvzcchyrbreymcgz1vl 
Host:localhost:61751 
Origin:http://localhost:61751 
Referer:http://localhost:61751/Transaction/4485 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5 
X-Requested-With:XMLHttpRequest 

**Query String Parameters** - view URL encoded 
key:4485 
_:1340830508447 
Form Dataview URL encoded 
page:1 
size:5 

**Response Headers** - view source 
Cache-Control:private 
Connection:Close 
Content-Length:21 
Content-Type:application/json; charset=utf-8 
Date:Wed, 27 Jun 2012 20:55:28 GMT 
Server:ASP.NET Development Server/10.0.0.0 
X-AspNet-Version:4.0.30319 
X-AspNetMvc-Version:3.0 

下面是从呼叫的头这是失败的(这是jQuery调用):

Request URL:http://localhost:61751/Cost/GetPurchaseOrdersComponent?id=1 
Request Method:GET 
Status Code:500 Internal Server Error 

**Request Headers** - view source 
Accept:text/plain, */*; q=0.01 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Type:application/x-www-form-urlencoded 
Cookie:ASP.NET_SessionId=uxn1ctvzcchyrbreymcgz1vl 
Host:localhost:61751 
Referer:http://localhost:61751/Transaction/4485 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5 
X-Requested-With:XMLHttpRequest 

**Query String Parameters** - view URL encoded 
id:1 

**Response Headers** - view source 
Cache-Control:private 
Connection:Close 
Content-Length:10434 
Content-Type:text/html; charset=utf-8 
Date:Wed, 27 Jun 2012 20:55:25 GMT 
Server:ASP.NET Development Server/10.0.0.0 
X-AspNet-Version:4.0.30319 
+0

由于您要返回一个“字符串”,所以您需要将dataType:更改为“text”。 –

+0

更改后没有修复错误500,尽管我发现这是需要做出的更改,所以谢谢。 – birdus

+0

我用dataType ='text'测试了你的代码,它在本地服务器上工作。建议参考MVC 3 url的方法是使用@ Url.Content(“〜/ Cost/GetPurchaseOrdersComponent”),你也可以在返回“some string”语句时加一个断点并添加一个“error :函数(a,b,c){...}“给ajax调用。 –

回答

1

这工作。不要问我为什么。

$.ajax({ 
    url: '@Url.Action("GetPurchaseOrdersComponent", "Cost", new { id = ViewBag.CustomerEstimateKey })', 
    type: "GET", 
    dataType: "text", 
    async: true, 
    success: function (result) { 
     $("#ComponentsMultiLevelGrid").html(result); 
    } 
}); 
+0

你工作的原因是他正在返回一个字符串,并且你使用了dataType:“text”,但他指定了dataType:“json ...”。字符串“some string”不是有效的json,因此将无法解析。 – AaronLS

1

JS ON请求仅适用于POST,因此您需要使用正确的动词并且还需要发送JSON请求,因为这是您在contentType参数中指定的内容。这与JSON.stringify方法实现:

$.ajax({ 
    url: "/Cost/GetPurchaseOrdersComponent", 
    type: "POST", 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    async: true, 
    data: JSON.stringify({ id: 1 }), 
    success: function (result) { 
     $("#ComponentsMultiLevelGrid").html(result); 
    } 
}); 

,或者如果你不想使用JSON请求摆脱contentType参数:

$.ajax({ 
    url: "/Cost/GetPurchaseOrdersComponent", 
    type: "GET", 
    dataType: "json", 
    async: true, 
    data: { id: 1 }, 
    success: function (result) { 
     $("#ComponentsMultiLevelGrid").html(result); 
    } 
}); 
+0

是的,你完全正确@Darin Dimitrov – lucask

+0

你的第二个例子实际上是我原来的。我不在乎使用json。我只需要在我的控制器中获取该整数。添加contentType参数就是我试过的东西。我也尝试过使用POST的json例子,但它仍然无法工作。 – birdus

+0

顺便说一句,这里是路线,但正如我所说的,它与使用Telerik网格的路线没什么两样:routes.MapRoute(null,“Cost/GetPurchaseOrdersComponent/{id}”,new {controller =“Cost “,action =”GetPurchaseOrdersComponent“}); – birdus