Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX, GET ok, POST not

Ajax GET requests are working properly. But I have to use POST because I am expecting to sent a larger amount of data, too much for GET.

Environment: Apache 2, Debian 9 (from scratch), jQuery 3.2.1, nothing special.

I stripped down my problem to this code:

CLIENT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="de">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Ajaxtest</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
    <script language="JavaScript">
    <!--
    $.ajax({
        url: 'ajaxtest2.php',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        type: 'POST',
        data: {testdata: 'here I am'},
        success: function (resp) {
            console.log(resp);
        },
    });
    -->
    </script>
</body>
</html>

SERVER

<?php
ini_set('error_reporting', E_ERROR);
header('Content-type: application/json'); 
header('HTTP/1.1 200 OK');
print json_encode(
    array(
        'method'=>$_SERVER['REQUEST_METHOD'],
        'get'=>$_GET['testdata'],
        'post'=>$_POST['testdata'])
    );
exit();
?>

When sending the ajax call via GET I only change

type: 'POST'

to

type: 'GET'

Which gives me this result on the console:

{method: "GET", get: "here I am", post: null}

This is what you would expect.

But when calling via POST I get:

{method: "POST", get: null, post: null}

The server understands a POST request but does not deliver any values.

I tried different ways of including the target url as advised by some with the same results

url: 'ajaxtest2.php'
url: './ajaxtest2.php'
url: './ajaxtest2.php/'

They all make no difference: $_POST stays empty.

Also I logged get_defined_vars() on the server, but $_POST stays empty and there is no trace of 'here I am' anywhere in the dumped variables.

There is no .htaccess mingling with url rewriting or the like.

What else can I do?

like image 453
Juergen Avatar asked Nov 07 '22 14:11

Juergen


1 Answers

The long answer for my comment that worked, is thusly:

You were trying to send data to the server using the contentType of:

application/json; charset=utf-8

Instead of the default and norm for sending POST data:

application/x-www-form-urlencoded; charset=UTF-8

On the server end, it was not taking the contentType as a form post, but rather, as application/json. This meant no data put into the $_POST variable for php to use.

GET is a different story, as that data rides in the URL, not in the body.

The encoding type should always be utf-8 for ajax calls, so that was not the issue either.

You shouldn't normally be wanting to send json data to a server like that. Its only appropriate if you are sending data to an application that is expecting raw json data that it is parsing on acceptance. For a PHP server, it wants 'form data' (otherwise you need to read php://input, see the bottom reference url below).

Hope that helps clear up the confusion.

Some more info: http://api.jquery.com/jquery.ajax/ (the contentType info) https://forum.jquery.com/topic/ajax-with-contenttype-application-json (last post is helpful)

like image 184
IncredibleHat Avatar answered Nov 14 '22 22:11

IncredibleHat