我是MVC的新手,并在MVC上编写Web应用程序。我正在尝试发送获取请求。
我得从我试图发送一个请求剃刀页:获取请求未发送
$(document).ready(function() {
var query = $('#productQuery').val();
alert("Start.");
var uri = 'api/search/products?q=abc';
$.get(uri, function (data) {
alert("Load was performed.");
});
});
而且我有链接到其工作绝对没问题的页面控制器,这ApiController:
[RoutePrefix("api/search")]
public class SearchController : ApiController
{
[Route("products")]
[HttpGet]
public async Task<IEnumerable<ProductDocument>> searchProducts(
[FromUri(Name = "q")]string query,
[FromUri(Name = "cat")]string[] categories=null)
{
ProductsResponse response = await SolrWebApi.QueryAsync<ProductsResponse>(query);
if (response.Header.Status != 0)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
return response.Body.Documents.Take(3).ToArray();
}
如果有人能告诉我我做错了什么,以及为什么我的请求没有发送,我将不胜感激。
给出路由属性,你的uri应该是'api/search/products'。您可能希望查看“@ Url.Action”以通过控制器+操作获取url,而不是硬编码路由。如果它仍然不起作用,请尝试不使用'categories'参数(或使用零参数并建立起来)。 –