wamp 3.1.7 does not handle "Content-Type", "application/json"
Posted by: peter75 (---.dip0.t-ipconnect.de)
Date: January 22, 2019 05:15AM

wamp server does not handle xhr.setRequestHeader("Content-Type", "application/json"winking smiley when using POST!
what is wrong? i already tried to config apache... no success.

i now have a html file:

<!DOCTYPE html>
<html>
<body>

<input id="clickMe" type="button" value="clickme" onclick="serverTestPost('bert');" />

<script>


    function serverTestPost(strTest){

        console.log("serverTestFunction"winking smiley;


        xhr = new XMLHttpRequest();
        //xhr.open("POST", departureReceiverAddress, true);
        xhr.open("POST", "postTestReceiver.php", true);
        //xhr.setRequestHeader("Content-Type", "application/json"winking smiley;
        //xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"winking smiley;
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"winking smiley;
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var json = JSON.parse(xhr.responseText);
                console.log(json.email + ", " + json.name)
            }
        }
        //xhr.send(data);
        //xhr.send("foo=bar&lorem=ipsum"winking smiley;
        var data = JSON.stringify({"email":"tomb@raider.com","name":"LaraCroft"});
        xhr.send(data);
    }
</script>

</body>
</html>

and a php

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>



Edited 1 time(s). Last edit at 01/22/2019 10:41AM by RiggsFolly.

Options: ReplyQuote
Re: wamp 3.1.7 does not handle "Content-Type", "application/json"
Posted by: RiggsFolly (Moderator)
Date: January 22, 2019 10:51AM

Hi

wamp server does not handle xhr.setRequestHeader("Content-Type", "application/json"winking smiley;

This is really not an acceptable problem report.

Please tell us what is happening.
What is not happening
What you believe should happen

PS Its a whole lot simpler to us jQuery to simplify AJAX coding

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: wamp 3.1.7 does not handle "Content-Type", "application/json"
Posted by: peter75 (---.dip0.t-ipconnect.de)
Date: January 22, 2019 01:51PM

hello mr. RiggsFolly,
it is not a acceptable problem report?

first: is there a way to disable the stupid smiley faces? oherwise it is not possible to post readable plain code... or is it?

you can see my very minimalistic code above.
1. simple html file . name does not matter.
2. a php file with only 6 lines - postTestReceiver.php.
it does not work.
there will be no post variable as long i am trying to set "Content-Type", "application/json";
that's why i disabled this line by comment.
by using "Content-Type", "application/x-www-form-urlencoded" there will be a post variable in php but you can not handle it like plain json...

i mean, this is really easy to try out and should be a really common issue ...???

i tried some "AddType 'application/json; charset=UTF-8' .json" in httpd.conf - cause i think there is no json type... but this does not work.

P.S. yupp i know jQuery.
the code above is a really short one.
and you don't need a additional library to make a xhr...

to make this easier to try out, i uploaded the testfiles to my dropbox.
[www.dropbox.com]

hope you will try it.
thy peter

Options: ReplyQuote
Re: wamp 3.1.7 does not handle "Content-Type", "application/json"
Posted by: RiggsFolly (Moderator)
Date: January 22, 2019 02:42PM

Hi,

Maybe you should just read this. It explains why you would not use `"Content-Type", "application/json"` when sending data to the server for PHP to process and why you would use `"Content-Type", "application/x-www-form-urlencoded"`

[stackoverflow.com]

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: wamp 3.1.7 does not handle "Content-Type", "application/json"
Posted by: peter75 (---.dip0.t-ipconnect.de)
Date: January 22, 2019 02:49PM

UGHHH.... i am sorry.
there is no diffrence in using jquery, BUT:
if you are using "Content-Type", "application/json" php is bypassing this and not showing it in $_POST var.
in oder to receive a plain json request with php you have to use:
$data = json_decode(file_get_contents('php://input'), true);
instead...
you CAN NOT USE $_POST variable, cause it shows no content if content-type is application/json!

so this is not a wamp error at all!!! - just not so well documented by php


thx peter

Options: ReplyQuote


Sorry, only registered users may post in this forum.