Angular 7 - http posts - no PAYLOAD sent inside headers [duplicate]












0















This question already has an answer here:




  • PHP Get JSON POST Data

    1 answer



  • Reading JSON POST using PHP

    3 answers



  • XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header

    6 answers




I just created an angular 7 project



I try to send a post with some data, nothing is set in the header part



so on the server side I only get the php script called, nothing in the $_POST array



this code works fine in angular 5, I should see the data in the header log in chrome



    createPostOptions() {
let headers = new Headers({
'Content-Type': 'application/json',
});

let options = new RequestOptions({ headers: headers, withCredentials: true });
return options;
}

getParts(): Observable<any>
{
return this.http.post('http://localhost/site/data.php',{command:'getParts'}, this.createPostOptions())
.pipe(map((response: Response) => {
return this.processData(response,this.router);
}));
}


php code:



function cors()
{
header("HTTP/1.1 " . "200" . " " . "OK");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header('Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, X-Requested-With, X-API-KEY, X-Auth-Token, X-Requested-With, Authorization, Content-Range, Content-Disposition, Origin, Access-Control-Request-Method');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Origin: '."http://localhost");
header('Access-Control-Allow-Credentials: true');
}

//-----------------------------------

if($_SERVER['REQUEST_METHOD']=="OPTIONS")
{
cors();
}
else
{
...
}


enter image description here
I should see something like this



enter image description here
Any help apreciated










share|improve this question















marked as duplicate by Quentin php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • where are you using createPostOptions ?
    – Pardeep Jain
    Nov 21 '18 at 14:43










  • 'Access-Control-Allow-Origin': '*' is nonsense. Access-Control-Allow-Origin is a response header, not a request header.
    – Quentin
    Nov 21 '18 at 15:03










  • @Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again
    – phil123456
    Nov 22 '18 at 12:08












  • @phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem.
    – Quentin
    Nov 22 '18 at 12:14












  • I updated my question, forgot about the cors indeed, but still not working
    – phil123456
    Nov 22 '18 at 12:21
















0















This question already has an answer here:




  • PHP Get JSON POST Data

    1 answer



  • Reading JSON POST using PHP

    3 answers



  • XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header

    6 answers




I just created an angular 7 project



I try to send a post with some data, nothing is set in the header part



so on the server side I only get the php script called, nothing in the $_POST array



this code works fine in angular 5, I should see the data in the header log in chrome



    createPostOptions() {
let headers = new Headers({
'Content-Type': 'application/json',
});

let options = new RequestOptions({ headers: headers, withCredentials: true });
return options;
}

getParts(): Observable<any>
{
return this.http.post('http://localhost/site/data.php',{command:'getParts'}, this.createPostOptions())
.pipe(map((response: Response) => {
return this.processData(response,this.router);
}));
}


php code:



function cors()
{
header("HTTP/1.1 " . "200" . " " . "OK");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header('Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, X-Requested-With, X-API-KEY, X-Auth-Token, X-Requested-With, Authorization, Content-Range, Content-Disposition, Origin, Access-Control-Request-Method');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Origin: '."http://localhost");
header('Access-Control-Allow-Credentials: true');
}

//-----------------------------------

if($_SERVER['REQUEST_METHOD']=="OPTIONS")
{
cors();
}
else
{
...
}


enter image description here
I should see something like this



enter image description here
Any help apreciated










share|improve this question















marked as duplicate by Quentin php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • where are you using createPostOptions ?
    – Pardeep Jain
    Nov 21 '18 at 14:43










  • 'Access-Control-Allow-Origin': '*' is nonsense. Access-Control-Allow-Origin is a response header, not a request header.
    – Quentin
    Nov 21 '18 at 15:03










  • @Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again
    – phil123456
    Nov 22 '18 at 12:08












  • @phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem.
    – Quentin
    Nov 22 '18 at 12:14












  • I updated my question, forgot about the cors indeed, but still not working
    – phil123456
    Nov 22 '18 at 12:21














0












0








0








This question already has an answer here:




  • PHP Get JSON POST Data

    1 answer



  • Reading JSON POST using PHP

    3 answers



  • XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header

    6 answers




I just created an angular 7 project



I try to send a post with some data, nothing is set in the header part



so on the server side I only get the php script called, nothing in the $_POST array



this code works fine in angular 5, I should see the data in the header log in chrome



    createPostOptions() {
let headers = new Headers({
'Content-Type': 'application/json',
});

let options = new RequestOptions({ headers: headers, withCredentials: true });
return options;
}

getParts(): Observable<any>
{
return this.http.post('http://localhost/site/data.php',{command:'getParts'}, this.createPostOptions())
.pipe(map((response: Response) => {
return this.processData(response,this.router);
}));
}


php code:



function cors()
{
header("HTTP/1.1 " . "200" . " " . "OK");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header('Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, X-Requested-With, X-API-KEY, X-Auth-Token, X-Requested-With, Authorization, Content-Range, Content-Disposition, Origin, Access-Control-Request-Method');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Origin: '."http://localhost");
header('Access-Control-Allow-Credentials: true');
}

//-----------------------------------

if($_SERVER['REQUEST_METHOD']=="OPTIONS")
{
cors();
}
else
{
...
}


enter image description here
I should see something like this



enter image description here
Any help apreciated










share|improve this question
















This question already has an answer here:




  • PHP Get JSON POST Data

    1 answer



  • Reading JSON POST using PHP

    3 answers



  • XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header

    6 answers




I just created an angular 7 project



I try to send a post with some data, nothing is set in the header part



so on the server side I only get the php script called, nothing in the $_POST array



this code works fine in angular 5, I should see the data in the header log in chrome



    createPostOptions() {
let headers = new Headers({
'Content-Type': 'application/json',
});

let options = new RequestOptions({ headers: headers, withCredentials: true });
return options;
}

getParts(): Observable<any>
{
return this.http.post('http://localhost/site/data.php',{command:'getParts'}, this.createPostOptions())
.pipe(map((response: Response) => {
return this.processData(response,this.router);
}));
}


php code:



function cors()
{
header("HTTP/1.1 " . "200" . " " . "OK");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header('Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, X-Requested-With, X-API-KEY, X-Auth-Token, X-Requested-With, Authorization, Content-Range, Content-Disposition, Origin, Access-Control-Request-Method');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Origin: '."http://localhost");
header('Access-Control-Allow-Credentials: true');
}

//-----------------------------------

if($_SERVER['REQUEST_METHOD']=="OPTIONS")
{
cors();
}
else
{
...
}


enter image description here
I should see something like this



enter image description here
Any help apreciated





This question already has an answer here:




  • PHP Get JSON POST Data

    1 answer



  • Reading JSON POST using PHP

    3 answers



  • XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header

    6 answers








php angular post angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:22









Goncalo Peres

1,3261318




1,3261318










asked Nov 21 '18 at 13:52









phil123456

265




265




marked as duplicate by Quentin php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Quentin php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • where are you using createPostOptions ?
    – Pardeep Jain
    Nov 21 '18 at 14:43










  • 'Access-Control-Allow-Origin': '*' is nonsense. Access-Control-Allow-Origin is a response header, not a request header.
    – Quentin
    Nov 21 '18 at 15:03










  • @Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again
    – phil123456
    Nov 22 '18 at 12:08












  • @phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem.
    – Quentin
    Nov 22 '18 at 12:14












  • I updated my question, forgot about the cors indeed, but still not working
    – phil123456
    Nov 22 '18 at 12:21


















  • where are you using createPostOptions ?
    – Pardeep Jain
    Nov 21 '18 at 14:43










  • 'Access-Control-Allow-Origin': '*' is nonsense. Access-Control-Allow-Origin is a response header, not a request header.
    – Quentin
    Nov 21 '18 at 15:03










  • @Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again
    – phil123456
    Nov 22 '18 at 12:08












  • @phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem.
    – Quentin
    Nov 22 '18 at 12:14












  • I updated my question, forgot about the cors indeed, but still not working
    – phil123456
    Nov 22 '18 at 12:21
















where are you using createPostOptions ?
– Pardeep Jain
Nov 21 '18 at 14:43




where are you using createPostOptions ?
– Pardeep Jain
Nov 21 '18 at 14:43












'Access-Control-Allow-Origin': '*' is nonsense. Access-Control-Allow-Origin is a response header, not a request header.
– Quentin
Nov 21 '18 at 15:03




'Access-Control-Allow-Origin': '*' is nonsense. Access-Control-Allow-Origin is a response header, not a request header.
– Quentin
Nov 21 '18 at 15:03












@Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again
– phil123456
Nov 22 '18 at 12:08






@Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again
– phil123456
Nov 22 '18 at 12:08














@phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem.
– Quentin
Nov 22 '18 at 12:14






@phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem.
– Quentin
Nov 22 '18 at 12:14














I updated my question, forgot about the cors indeed, but still not working
– phil123456
Nov 22 '18 at 12:21




I updated my question, forgot about the cors indeed, but still not working
– phil123456
Nov 22 '18 at 12:21












1 Answer
1






active

oldest

votes


















0














Since you are sending json in the body, but not as post parameters, you need something like



$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;





share|improve this answer





















  • that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
    – phil123456
    Nov 22 '18 at 11:57












  • but it only solves php part, the issue is on angular side too ...no data is sent in the header
    – phil123456
    Nov 22 '18 at 12:06










  • @phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
    – David
    Nov 22 '18 at 12:16










  • just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
    – phil123456
    Nov 22 '18 at 12:23










  • apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
    – phil123456
    Nov 22 '18 at 12:33


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Since you are sending json in the body, but not as post parameters, you need something like



$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;





share|improve this answer





















  • that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
    – phil123456
    Nov 22 '18 at 11:57












  • but it only solves php part, the issue is on angular side too ...no data is sent in the header
    – phil123456
    Nov 22 '18 at 12:06










  • @phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
    – David
    Nov 22 '18 at 12:16










  • just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
    – phil123456
    Nov 22 '18 at 12:23










  • apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
    – phil123456
    Nov 22 '18 at 12:33
















0














Since you are sending json in the body, but not as post parameters, you need something like



$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;





share|improve this answer





















  • that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
    – phil123456
    Nov 22 '18 at 11:57












  • but it only solves php part, the issue is on angular side too ...no data is sent in the header
    – phil123456
    Nov 22 '18 at 12:06










  • @phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
    – David
    Nov 22 '18 at 12:16










  • just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
    – phil123456
    Nov 22 '18 at 12:23










  • apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
    – phil123456
    Nov 22 '18 at 12:33














0












0








0






Since you are sending json in the body, but not as post parameters, you need something like



$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;





share|improve this answer












Since you are sending json in the body, but not as post parameters, you need something like



$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 14:47









David

10.7k63451




10.7k63451












  • that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
    – phil123456
    Nov 22 '18 at 11:57












  • but it only solves php part, the issue is on angular side too ...no data is sent in the header
    – phil123456
    Nov 22 '18 at 12:06










  • @phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
    – David
    Nov 22 '18 at 12:16










  • just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
    – phil123456
    Nov 22 '18 at 12:23










  • apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
    – phil123456
    Nov 22 '18 at 12:33


















  • that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
    – phil123456
    Nov 22 '18 at 11:57












  • but it only solves php part, the issue is on angular side too ...no data is sent in the header
    – phil123456
    Nov 22 '18 at 12:06










  • @phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
    – David
    Nov 22 '18 at 12:16










  • just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
    – phil123456
    Nov 22 '18 at 12:23










  • apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
    – phil123456
    Nov 22 '18 at 12:33
















that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
– phil123456
Nov 22 '18 at 11:57






that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-)
– phil123456
Nov 22 '18 at 11:57














but it only solves php part, the issue is on angular side too ...no data is sent in the header
– phil123456
Nov 22 '18 at 12:06




but it only solves php part, the issue is on angular side too ...no data is sent in the header
– phil123456
Nov 22 '18 at 12:06












@phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
– David
Nov 22 '18 at 12:16




@phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly?
– David
Nov 22 '18 at 12:16












just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
– phil123456
Nov 22 '18 at 12:23




just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging
– phil123456
Nov 22 '18 at 12:23












apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
– phil123456
Nov 22 '18 at 12:33




apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent
– phil123456
Nov 22 '18 at 12:33



Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'