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
由於您要返回一個「字符串」,所以您需要將dataType:更改爲「text」。 –
更改後沒有修復錯誤500,儘管我發現這是需要做出的更改,所以謝謝。 – birdus
我用dataType ='text'測試了你的代碼,它在本地服務器上工作。建議參考MVC 3 url的方法是使用@ Url.Content(「〜/ Cost/GetPurchaseOrdersComponent」),你也可以在返回「some string」語句時加一個斷點並添加一個「error :函數(a,b,c){...}「給ajax調用。 –