I want to make service in Angular2 which can make http call to different
Cross origin with 'Content-Type', 'application/x-www-form-urlencoded' and
get json response in Angular2 array or any object.
Basically How to submit Formdata using JSONP for cross-domain
/**
* Created by rkumar on 5/16/2016.
*/
import {Injectable} from "@angular/core";
import {Jsonp, URLSearchParams, Http, Response, Headers,
RequestOptions} from '@angular/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable'
@Injectable()
export class JsonDataService{
records: Object;
private jsonDataUrl =
"http://localhost:8080/serverUtilizationReport";
constructor(private http: Http){ }
getJsonData(){
var headers = new Headers();
headers.append('Content-Type',
'application/x-www-form-urlencoded');
headers.append('Access-Control-Allow-Origin','http://localhost:8080/serverUtilizationReport');
return
this.http.post(this.jsonDataUrl,JSON.stringify({TimeStamp:'2016-04-12
12:50:00'}),{headers:headers})
.map(this.extractData)
}
private extractData(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Response status: ' + res.status);
}
let body = res.json();
return body.data || { };
}
}
# But i am getting an error in Console as follows:
[image: image]
<https://cloud.githubusercontent.com/assets/11659536/15291202/54f25b2e-1b9a-11e6-9aeb-3837e0490df4.png>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.