【发布时间】:2014-06-28 02:00:27
【问题描述】:
我目前正在研究 ASP.NET WebApi 和 Angularjs
WebApi 有一个方法
[System.Web.Http.AcceptVerbs("POST")]
[System.Web.Http.HttpPost]
public HttpResponseMessage SearchAddress(SearchDetails searchDetail)
{
//13.03993,80.231867
try
{
if (!WebSecurity.IsAuthenticated)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NotAcceptable);
return response;
}
List<CollegeAddress> CollegeAddress = addressService.GetAddressFromDistance(17.380498, 78.4864948, 2000);
HttpResponseMessage responseData = Request.CreateResponse(HttpStatusCode.Accepted, CollegeAddress);
return responseData;
}
catch (Exception e)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NotFound);
return response;
}
}
我必须从客户端调用这个方法。
当我使用Ajax调用这个方法时,它不起作用,如果我使用Ajax,方法参数searchDetail总是为空。
$.ajax({
method: 'POST',
url: rootUrl + '/api/Address/SearchAddress',
async: false,
data: searchDetail,
type: "json",
headers: {
'Content-Type': "application/json; charset=utf-8"
}
}).success(function (response) {
return response;
}).error(function () {
toastr.error('Somthing is wrong', 'Error');
})
但是当我通过HTTP 请求调用该方法时,它正在工作。
$http({
method: 'POST',
url: rootUrl + '/api/Address/SearchAddress',
data: searchDetail,
headers: {
'Content-Type': "application/json; charset=utf-8"
}
}).success(function (response) {
toastr.success('Account Created successfully!', 'Account Created');
return response;
}).error(function () {
toastr.error('Somthing is wrong', 'Error');
})
为什么?它们之间有什么区别?为什么 Ajax 不工作而 HTTP 工作?
【问题讨论】:
-
我确定 some1 会回答,...但是你为什么将
$.ajax与 Angular 项目混合使用?我会完全删除 jQuery。 -
@MaximShoustin ,因为http不支持
async:false对象。我想同步调用一些方法。所以我用了一段时间的 Ajax -
我认为是服务器。如果他没有配置消费
application/json你不能发送这样的请求。和http?默认情况下。
标签: javascript ajax angularjs http asp.net-web-api