dir.by  
Поиск >>

Компьютер, программы
→
PHP (Programming Language for Fast Writing of a Web Site)
 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 
посмотрели 2613 раз
обновлено: 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
Regular expressions
 
Next topic →
Zend Framework (PHP framework)
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

Новость "Черная пятница" Dreame в Беларуси – две недели скидок до 3400 рублей
 
PHP Study
• What is 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
• 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