I am new to angular2. I want to make a http post using angular2.Ajax request for the same is-
$.ajax({
url: apiUrl,
type: 'Post',
dataType: "json",
data: request,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization-ApiKey", 'Ak12mr27Xwg@d89ul');
},
success: function (response) {
if (response.ErrorCode != 0) {
alert(response.ErrorMessage)
}
else {
}
},
error: function (a) {
alert('Invalid Action.');
}
});
Actualy, i don't know how to set beforesend header in angular2. I tried this but nothing works. its giving-
Failed to execute 'open' on 'XMLHttpRequest': Invalid URL" and
Response with status: 500 Internal Server Error for URL: http://111.118.241.110/B****i/api/R*****1/Login
My angular2 code for post api-
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map'
@Injectable()
export class AuthenticationService {
constructor(private http: Http) { }
login(username: string, password: string) {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Authorization-ApiKey','Ak12mr27Xwg@d89ul');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
return this.http.post('http://111.118.241.110/B****i/api/R******1/Login', {
UserName: username, Password: password } , options)
.map((response: Response) => {
let user = response;
});
}
}
I am very sure ,handled all CORS enable related issue for API.what could be the solution. P.S: Its working fine for GET request..