【发布时间】:2013-06-23 11:29:02
【问题描述】:
我有一个简单的 web api 控制器操作方法:
public class WeightController : ApiController
{
[HttpGet]
[AcceptVerbs("GET")]
public int GetWeight(int weightId)
{
return 5;
}
}
我使用 webapi 的默认路由配置
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我需要做跨域调用所以我使用jsonp调用:
$.ajax({
url: 'api/Weight/1',
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
alert('success:' + data);
},
error: function(jqXHR,status,error) {
alert('error');
}
});
我收到以下响应(代码 404):
"No HTTP resource was found that matches the request URI
'http://localhost:31836/api/Weight/1?callback=jQuery18204532131106388192_1372242854823&_=1372242854950'.",
"MessageDetail":"No action was found on the controller 'Weight' that matches the request."
映射此 jsnop 请求的正确操作方法定义应该是什么? 如您所见,jsonp 添加了回调参数。它是否也应该映射到动作参数中?那里无关紧要!
任何帮助表示赞赏 =]
【问题讨论】:
标签: jquery asp.net-web-api cross-domain jsonp asp.net-web-api-routing