Chrome调试器显示名称为“GetPurchaseOrdersComponent”,路径显示为“/ Cost”,而Telerik网格控件发出的调用名称为“4485”,路径为“/ Cost / GetPurchaseOrders”(并且它们正在运行) 。此外,我的通话类型(在Chrome的调试器中查看时)是text / html,而在工作调用中,它是application / json。我得到的错误是:“500(内部服务器错误)”。我为此调用定义了与其他调用类似的路由。这是我的代码:
$.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";
}
更新
以下是有效呼叫的标头(此呼叫来自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 :(得分:1)
JSON请求仅适用于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);
}
});
答案 1 :(得分:1)
这很有效。不要问我为什么。
$.ajax({
url: '@Url.Action("GetPurchaseOrdersComponent", "Cost", new { id = ViewBag.CustomerEstimateKey })',
type: "GET",
dataType: "text",
async: true,
success: function (result) {
$("#ComponentsMultiLevelGrid").html(result);
}
});