dir.by  
  Search  
Programming, development, testing
PHP (язык программирования для быстрого написания Web сайта)
Create an html file and javascript sends a post request to the php server. PHP server makes the answer | Why on php server $_REQUEST or $_POST array is empty
  Looked at 638 times    
 Create an html file and javascript sends a post request to the php server. PHP server makes the answer | Why on php server $_REQUEST or $_POST array is empty 
last updated: 12 February 2025
Step 1. Create an html file and inside javascript
  Html  
<script>
     var url = "https://dir.by/developer/myapi/login.php";

     var params = "login=evgen&password=12345";

     xmlHttpReq = new XMLHttpRequest();
     xmlHttpReq.open('POST', url, true);
     xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xmlHttpReq.onreadystatechange = function()
     {
          if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) // success
          {
               alert(xmlHttpReq.responseText);
          }
     }
     xmlHttpReq.send(params);
</script>
Step 2. The server receives the post request. The server creates a response here in the login.php file
<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

header('Content-Type: application/json');

$response = array();
$response['success'] = false;

if ($_REQUEST['login']=='evgen' && $_REQUEST['password']=='12345')
{
     $response['success'] = true;
     $response['token'] = "123123dasa213ws212";
}

echo json_encode($response);

?>
Note! If on php server $_REQUEST or $_POST array is empty when sent via javascript
Decision: When JavasScript sends a request, it should use Content-Type like this application/x-www-form-urlencoded
  Html  
<script>
     var url = "https://dir.by/developer/myapi/login.php";

     var params = "login=evgen&password=12345";

     xmlHttpReq = new XMLHttpRequest();
     xmlHttpReq.open('POST', url, true);
     xmlHttpReq.setRequestHeader('Content-Type', 'application/json' 'application/x-www-form-urlencoded');
     xmlHttpReq.onreadystatechange = function()
     {
          if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) // success
          {
               alert(xmlHttpReq.responseText);
          }
     }
     xmlHttpReq.send(params);
</script>
 
← Previous topic
Регулярные выражения
 
Next topic →
Zend Framework (PHP framework)
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
PHP Study
Что такое PHP ?
Function
Function in PHP. Example: function CalculateSum($value1, $value2) { ... }
Passing parameters by value and by reference to a function in PHP
Pass a function as a parameter to a function | PHP
Unnamed function
Anonymous function (no name). Function pointer in PHP
Pass the unnamed function as a parameter to the | using use (...) the unnamed function sees the variable above | PHP
Arrays
Array in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
Look for an element in an array in PHP | array_search
Key-value arrays
Array (key-value) in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
Regular expressions
Регулярные выражения
PHP Server
Create an html file and javascript sends a post request to the php server. PHP server makes the answer | Why on php server $_REQUEST or $_POST array is empty
Framworks (frameworks)
Zend Framework (PHP framework)
Yii (PHP framework)
CakePHP (framework)

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика