{"id":454,"date":"2017-11-07T12:54:16","date_gmt":"2017-11-07T12:54:16","guid":{"rendered":"http:\/\/www.codeinsightacademy.com\/blog\/?p=454"},"modified":"2019-10-09T03:14:26","modified_gmt":"2019-10-09T03:14:26","slug":"angular-4-http-post-with-php","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/c-programming\/angular-4-http-post-with-php\/","title":{"rendered":"Angular 4 http post with php"},"content":{"rendered":"<p>Component or Service:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let data = {'title': 'foo',\t'body': 'bar', 'userId': 1};\r\n    this.http.post('http:\/\/localhost\/angular4\/angcrud\/add_student_action.php', data)\r\n      .subscribe(\r\n        (res:Response) =&gt; {\r\n          console.log(res.json());\r\n        },\r\n        err =&gt; {\r\n          console.log(\"Error occured\");\r\n        }\r\n      );<\/pre>\n<p>&nbsp;<\/p>\n<p>Php Script:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n\r\n\r\n$postdata = file_get_contents(\"php:\/\/input\");\r\n$request = json_decode($postdata);\r\n\r\nheader('Content-type: application\/json');\r\nheader(\"Access-Control-Allow-Origin: *\");\r\nheader('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');\r\n\/\/$res_ar = array(\"foo\"=&gt; $_REQUEST['body']);\r\n\r\n\r\necho json_encode($request);\r\n\r\n?&gt;\r\n\r\n\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;form #studentForm = \"ngForm\" (ngSubmit)=\"addStudentRecord(studentForm.value)\"&gt;\r\n  First name:&lt;br&gt;\r\n  &lt;input type=\"text\" name=\"firstname\" value=\"Mickey\" ngModel&gt;\r\n  &lt;br&gt;\r\n  Last name:&lt;br&gt;\r\n  &lt;input type=\"text\" name=\"lastname\" value=\"Mouse\" ngModel&gt;\r\n  &lt;br&gt;&lt;br&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Component or Service: let data = {&#8216;title&#8217;: &#8216;foo&#8217;, &#8216;body&#8217;: &#8216;bar&#8217;, &#8216;userId&#8217;: 1}; this.http.post(&#8216;http:\/\/localhost\/angular4\/angcrud\/add_student_action.php&#8217;, data) .subscribe( (res:Response) =&gt; { console.log(res.json()); }, err =&gt; { console.log(&#8220;Error occured&#8221;); } ); &nbsp; Php Script: &lt;?php $postdata = file_get_contents(&#8220;php:\/\/input&#8221;); $request = json_decode($postdata); header(&#8216;Content-type: application\/json&#8217;); header(&#8220;Access-Control-Allow-Origin: *&#8221;); header(&#8216;Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers&#8217;); \/\/$res_ar = array(&#8220;foo&#8221;=&gt; $_REQUEST[&#8216;body&#8217;]); echo json_encode($request); ?&gt; &lt;form [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/454"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=454"}],"version-history":[{"count":4,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":832,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/454\/revisions\/832"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}