Check out our Angular Book Series.

How do I send JSON to a Server in Angular 2+?

I have a lot more trouble answering Angular questions on StackOverflow than I did answering Flex questions. The bulk of Angular problems seem to come from integrating different systems and libraries together. But, every once in a while I see a question that I seem to be the perfect person to answer. This question is about Sending JSON from Angular to PHP. I wrote a book about that.

When sending JSON to PHP--or Java or ColdFusion or any server side tech--you need to be sure to set the headers to application/json.

If you are using Angular 2, this is how you do it:

let optionHeaders : Headers = new Headers();
optionHeaders.append('Content-Type', 'application/json');
let options: RequestOptions = new RequestOptions({headers:optionHeaders});
this.http.post(serverEndPoint, JSONPayload,options);

We create an instance of a RequestOptions object, and add the Headers onto it. The RequestOptions object is sent as part of your server request, in this case using a post() method. This is all part of the now deprecated Http library.

Angular 5 introduced the new HttpClient library, which does things a bit differently:

let options : Object;
let optionHeaders : HttpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
options = {headers:optionHeaders};
this.http.post(serverEndPoint, JSONPayload,options);

You create a generic options object, and add an HttpHeaders() instance inside of it. Send that as the options argument to the http method, also a post().

The Learn With Series now includes Java and PHP

The Learn With series now includes a few new book that talk about integrating with Java or PHP.

Check them out now.

I'm greatly enjoying writing these books, experimenting with different technologies, and taking you all along for the ride.

How do I create a REST service in PHP?

I've been working to update the LearnWith series. The series builds UI applications--right now with Angular--and multiple backends with NodeJS, or ColdFusion. I'm creating a new book that creates services in PHP.

How do you create a REST service in PHP?

I did it this way. First, add a header:


header("Content-Type: application/json; charset=UTF-8");

The Content-Type header says the results will be in JSON.

Then, get the request type:


$method = $_SERVER['REQUEST_METHOD'];

The $method variable will be GET, or POST, or PUT, or whatever REST protocol you're using.

Then, use a switch statement to determine the type of request:

switch ($method) {
case 'GET':
// access URL Variables
// $_GET["myvar"])
// data retrieval here;
echo(json_encode($result));
break;
case 'PUT':
// retrieve PUT body data
$data = json_decode(file_get_contents('php://input'));
// $data->
myvar
// Update data Here
echo(json_encode($result) );
break;
case 'POST':
// retrieve POST Body data
$data = json_decode(file_get_contents('php://input'));
// $data->myvar
// Create some data Here
echo(json_encode($result) );
break;
default:
echo("Unknown Request Type");
break;
}

That is pretty much it. For get requests, you can access URL variables directly, like this:


$_GET["myvar"])

For PUT or POST requests, you'll most likely be sending a JSON object as the body of the request and you can access it like this:


$data = json_decode(file_get_contents('php://input'));

I hope this helps someone. Be sure to check out my LearnWith series, where you'll be able to find information about integrating Angular and AngularJS with PHP very soon.

All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.