AJAX, GET ok, POST not

Ajax GET requests are working properly. But I have to use POST because I expect to send more data, too much for GET.

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

I split my problem into this code:

CUSTOMER

<!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 a call, ajax via GET only changes

 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 expect.

But when called through POST, I get:

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

The server understands the POST request, but does not pass any values.

I tried different ways to include the destination URL as someone with the same results recommended

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

They all do not matter: $ _POST remains empty.

I also ran get_defined_vars () on the server, but $ _POST remains empty, and there is no trace of 'here I am' anywhere in the resettable variables.

There is no .htaccess mixed with URL rewriting or the like.

What else can I do?

+5
source share
1 answer

The long answer for my comment that worked is as follows:

You tried to send data to the server using contentType:

 application/json; charset=utf-8 

Instead of the default and the norm for sending POST data:

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

At the end of the server, it did not accept contentType as a form message, but rather as application/json . This meant that no data was placed in the $ _POST variable to use php.

GET is a different story, as this data goes to the URL, not the body.

The encoding type should always be utf-8 for ajax calls, so this is not a problem either.

Normally you should not send json data to a server such as . It can only be used if you send data to an application that expects raw json data, which it parses upon acceptance. For the PHP server, he wants "form data" (otherwise you need to read php://input , see the lower link url below).

Hope this helps resolve the confusion.

Additional information: http://api.jquery.com/jquery.ajax/ (information contentType) https://forum.jquery.com/topic/ajax-with-contenttype-application-json (last post helpful)

+1
source

Source: https://habr.com/ru/post/1273525/


All Articles