I've uploaded "mytest.html" to the IIS server, and this project consists of WebApi and AngularJS. However, I am unable to make a correct request to my WebApi. I'm not sure why?
【HTML Code Snippets】
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
</head>
<body>
<div ng-app="A" ng-controller="Test">
<form method='post' name='myForm'>
<table width="100%">
<tr>
<td>Name:</td>
<td><input type='text' ng-model='user.name' name='myname' required/>
<span style='color:red' ng-show='myForm.myname.$dirty && myForm.myname.$invalid'>Name cannot be empty!</span>
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<div ng-repeat='s in sexs'>
<input type='radio' ng-model='user.sex' name='mysex' value='{{s}}'>
{{s}}
</div>
</td>
</tr>
<tr>
<td>Hobby:</td>
<td>
<select ng-model='user.love' name='selLove'>
<option ng-repeat='l in loves' value='{{l}}'>{{l}}</option>
</select>
<span style='color:red' ng-show='selChoice(myForm.selLove.$dirty);'>You cannot choose "Please choose"</span>
</td>
</tr>
</table>
<input type ='submit' value='Submit' ng-click='isDisable();'/>
</form>
</div>
<script>
var app = angular.module('A',[],function($httpProvider){
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
});
app.controller('Test',function($scope,$http){
$scope.user = {};
$scope.sexs = ['M','F'];
$scope.loves = ['Please choose','Programming','Piano','Swimming'];
$scope.user.sex = 'M';
$scope.user.love = 'Please choose';
$scope.selChoice= function(isDirty){
var isFalse = (myForm.selLove.value == 'Please choose' && isDirty);
$scope.myForm.selLove.$invalid = isFalse;
$scope.myForm.$invalid |= isFalse;
return isFalse;
};
$scope.isDisable = function(){
$scope.selChoice(true);
var requestData = $.param($scope.user);
alert(requestData);
if($scope.myForm.$invalid){
alert('Invalid form');
}
else{
$http({
method:'POST',
data: requestData,
url:'http://localhost:60031/api',
success: function (data){
alert(JSON.stringify(data));
}
});
}
};
});
</script>
</body>
</html>
【WebApi Code Snippets】
namespace AngularJSDemo.Controllers
{
using Models;
using System.Web.Http;
public class DefaultController : ApiController
{
[HttpPost]
public Person GetPerson([FromBody] Person p)
{
return p;
}
}
}
【Model Code Snippets】
namespace AngularJSDemo.Models
{
public class Person
{
public string Name { get; set; }
public string Sex { get; set; }
public string Love { get; set; }
}
}
【Register Code Snippets】
namespace AngularJSDemo
{
using Newtonsoft.Json.Serialization;
using System.Net.Http.Formatting;
using System.Web.Http;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
var jsonFormatInstance = new JsonMediaTypeFormatter();
jsonFormatInstance.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.Add(jsonFormatInstance);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, controller="Default" }
);
}
}
}
The error displayed in the console of my Google Chrome is:
angular.js:10695 POST http://localhost:60031/api 415 (Unsupported Media Type)
I also attempted to use JSON format but without any success :(
$http({
method:'POST',
data: {p:$scope.user},
url:'http://localhost:60031/api/Default',
success: function (data){
alert(JSON.stringify(data));
}
});
And the Content-Type set is as follows:
var app = angular.module('A',[],function($httpProvider){
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json';
});
Due to having different ports and domains, I also made these adjustments:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Request-Methods" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>