AJAX POST JSON Error for PHP No DATA in PHP

I'm having trouble sending data $_POSTthrough jQuery Ajax. I read everything I can find on this subject, and still I won’t go anywhere. I even returned to very simple data and still nothing, the JSON data was verified using the JSON validation site. For life, I cannot echo/ print_r/ var_dumpor access any data.

My JavaScript:

$('#updateJSON').click(function(){
    var content = [{
                        "id": 21,
                        "children": [{
                            "id": 196
                        }, {
                            "id": 195
                        }, {
                            "id": 49
                        }, {
                            "id": 194
                        }]
                }];
    var someText = "someText";
        $.ajax({
             type: 'POST',
             url: 'test.php',
             data: {data: content},
             cache: false,
             success: function(){  
                 alert("success");
                 console.log(content);
                 window.location = "test.php";
             },
             error:function(){
                 alert('ajax failed');    
             }   
        }); 
}); 

My PHP:

<?php
if(isset($_POST['data'])) {
    $json = stripslashes($_POST['data']);
    var_dump(json_decode($json, true));
} else {
    echo "No Data";
}
?>

So, I get alert("success"), and then redirected test.php once when test.phpI get the "No data" echo.

Thank you in advance for your help in resolving this issue.

+4
source share
4 answers

, .

test.php a, $json = array(), $json [] = json_decode ($ _ POST ['data']);

JSON.

php-:

<?php
$json = array();
if($_POST['data']) {
$json[] = json_decode($_POST['data']);
file_put_contents("../JSON/test.json",json_encode($json)); 
} else {
echo "No Data";
}
?>

"JSON/test.json", JSON, JS.

:

JSON.stringify(content); NULL, @Rahul Patel , .

JSON JSON.

JS:

$('#updateJSON').click(function(){
    var content = {
                        "id": 21,
                        "children": [{
                            "id": 196
                        }, {
                            "id": 195
                        }, {
                            "id": 49
                        }, {
                            "id": 194
                        }]
                };

        $.ajax({
             type: 'POST',
             url: 'test.php',
             data: {data: JSON.stringify(content)},
             cache: false,
             success: function(){  
             alert("JSON Updated");
             },
             error:function(){
                 alert('ajax failed');    
             }   
        }); 

}); 
+1

, window.location PHP ( - ) test.php.

ajax test.php.

test.php success.

$('#updateJSON').click(function(){
    var content = [{
                        "id": 21,
                        "children": [{
                            "id": 196
                        }, {
                            "id": 195
                        }, {
                            "id": 49
                        }, {
                            "id": 194
                        }]
                }];
    var someText = "someText";


$.ajax({
         type: 'POST',
         url: 'test.php',
         data: {data: content},
         cache: false,
         success: function(response){  
             alert("success");
             console.log(response);
             //window.location = "test.php";
         },
         error:function(){
             alert('ajax failed');    
         }   
  }); 
+3

ajax. JSON.stringify(conten) json ajax.

data: {data: JSON.stringify(content)}

data: {data: content}
+2

:

<?php
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);

:

       $field1 = @$request->field1Name;
       $field2 = @$request->field2Name;
etc..

EDIT:

, .

, , :

Javascript: - JSON "ajax",

, "" , test.php

$(document).ready(function(){
    $('#updateJSON').click(function(){
        var content = {
            "id": 21,
            "children": [{
                "id": 196
            }, {
                "id": 195
            }, {
                "id": 49
            }, {
                "id": 194
            }]
        };


        var someText = "someText";
        $.ajax({
            type: 'POST',
            url: 'test.php',
            data:  JSON.stringify(content),
            cache: false,
            success: function(data){
                alert(data);
                console.log(content);
            },
            error:function(){
                alert('ajax failed');
            }
        });
    });
});
Hide result

PHP:

<?php

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$field1 = @$request->id;
echo $field1;

$field1 "21",

.

+2

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


All Articles