i M doing post request from angular 2 with asp.net webapi 2 when i m running my app. it giving me above error message i checked its response in fiddler as
i search on google for the solution and made changes according to suggestion but still i m facing the same error. Asp.net web api code is:-
public void postvalidUser(string UserName,string Password)
{
try
{
var login = uData.Users.Where(x => x.UserName == UserName).ToList();
if (login != null && login.Count > 0)
{
foreach (var log in login)
{
if (log.UserName == UserName && log.Password == Password)
{
Console.WriteLine("login ok");
}
}
}
else
{
Console.WriteLine("login fail");
}
}
catch (Exception ex){
Console.WriteLine(ex);
}
}
login.servuce.ts code
@Injectable()
export class LoginService {
constructor(private _http: Http) { }
postvalidUser(UserName: string, Password: string) {
const users = { UserName: UserName, Password: Password };
let body = JSON.stringify(users);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers: headers });
return this._http
.post("http://localhost:65440/api/User", users, options)
.map(result => result.json());
}
}
login.component.ts
export class LoginComponent implements OnInit {
constructor(private _loginservices: LoginService) { }
ngOnInit() {
}
OnClick(UserName: string, Password:string) {
this._loginservices.postvalidUser(UserName, Password)
.subscribe(users => console.log(users));
}
login.component.html
<div>
<table>
<tr>
<td>UserName</td><td><input [(ngModel)]="UserName" /></td>
</tr>
<tr><td>Password</td><td><input [(ngModel)]="Password" /></td>
</tr>
<tr>
<td></td><td><button class="btn btn-primary" (click)="OnClick(UserName.value,Password.value)">Submit</button></td>
</tr>
</table>
</div>
Please correct me if i have some error in my code