Tag: asp.net web api2

ASP.NET Web API 2 – POST / jQuery

我试图将一个对象发布到我使用Web API 2的服务器上。代码如下: $.ajax({ cache: false, type: “POST”, url: “/api/Employees”, data: { EmployeeId: 1 }, success: function(result) { console.log(“employees saved successfully”); }, error: function(result) { } }); 至于Web API: public class EmployeesController : ApiController { // POST api/ public void Post([FromBody]Employee value) { } } public class Employee { public Int32 EmployeeId { get; set; […]

Ajax:预检的响应具有无效的HTTP状态代码404

我有服务,当我打电话给他 我收到错误,预检的响应有无效的HTTP状态代码404,如下图所示。 请在这里解决这个问题谢谢 这是ajax $.ajax({ url:url, type: “Get”, async: true, contentType: “application/text”, dataType: “json”, cache: false, crossDomain: true, success: function (data) { } } }); Web.config文件

从本地Web表单应用程序进行本地web api调用

我想知道是否可以拨打这个电话: jQuery.ajax({ type: “POST”, url: “http://localhost:5832/api/Login”, data: “{username: user1, password:’123456′}”, contentType: “application/json; charset=utf-8”, dataType: “json”, success: function (data) { alert(data.d); } }); 这基本上是调用这个web api控制器: public class LoginController : ApiController { [System.Web.Http.AcceptVerbs(“POST”)] public HttpResponseMessage Post(string username, string password) { string authenticationToken = “”; Helpers hpl = new Helpers(); authenticationToken = hpl.LoginUser(username, password); int userId = 0; […]

在反序列化Web.API参数时未调用自定义Json.NET JsonConverter

我有一个Web.API端点,它将这样的对象作为参数: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public UserName UserName { get; set; } } 例如: [Route(“api/person”)] [AcceptVerbs(“POST”)] public void UpdatePerson(Person person) { // etc. } 我已经定义了一个自定义JsonConverter来从JSON字符串属性转换为我的自定义UserName类: public class UserNameJsonDeserializer : JsonConverter { public override bool […]

如何通过AngularJS $ http从ASP.Net Web API 2获取Access Token?

我试着这样: $http({ method: ‘POST’, url: ‘/token’, data: { username: $scope.username, password: $scope.password, grant_type: ‘password’ } }).success(function (data, status, headers, config) { $scope.output = data; }).error(function (data, status, headers, config) { $scope.output = data; }); 然后尝试将grant_type更改为param: $http({ method: ‘POST’, url: ‘/token’, data: { username: $scope.username, password: $scope.password }, params: { grant_type: ‘password’ } }).success(function (data, status, […]

提示文件下载

我在我的页面上有一个链接点击我试图生成PDF文档,然后在浏览器上显示“打开 – 保存”提示。 我的HTML(reactjs组件)具有以下代码,其中onclick调用_getMyDocument函数,然后调用Webapi方法。 Test Link _getMyDocument(e) { GetMyDocument(this.props.mydata).then(()=> { }).catch(error=> { }); 我的控制器具有以下代码 [HttpPost] [Route(“Generate/Report”)] public IHttpActionResult GetMyReport(MyData myData) { byte[] myDoc = MyBusinessObject.GenerateMyReport(myData); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(myDoc) }; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(“attachment”) { FileName = “MyDocument.pdf” }; result.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/octet-stream”); var response = ResponseMessage(result); return response; […]