【发布时间】:2012-05-28 09:31:09
【问题描述】:
在我的 Web Api 项目中,我有一个 Get 方法,它从我的数据源返回数据:
[WebGet(UriTemplate = "")]
public IQueryable<Product> Get()
{
var products = _db.Products;
return products.AsQueryable();
}
当我使用测试客户端在浏览器中查看此内容时,当我指定以 JSON 格式返回时,它可以正常工作并返回 JSON。
在我的另一个客户端上,由于我的 API 托管在同一个域上(仅用于开发),我必须创建一个 JSONP,但是我从我的 JSON 请求中收到的数据是 XML,我怎样才能让它返回为 JSON ?以下是我提出请求的代码:
$.ajax({
crossDomain: true,
dataType: "jsonp",
url: "http://localhost:9000/api/products",
contentType: 'application/json; charset=utf-8',
type: "get",
success: function (data) {
console.log(data);
}
});
【问题讨论】:
-
您似乎仍在使用现已过时的 WCF Web API,而不是较新的 ASP.NET Web API。见nuget.org/packages/AspNetWebApi
-
@DarrelMiller - 很好。我什至没有注意到
WebGet属性。 -
我已经把它换成使用 ASP.NET Web API 并使用
EBarr的答案
标签: jquery json asp.net-mvc-3 jsonp asp.net-web-api