简体   繁体   English

PHP无法获取AJAX JSON数据

[英]PHP doesn't get AJAX JSON data

Trying to get json array from ajax, but when i'm trying to write it down in the text file, it shows nothing. 试图从ajax获取json数组,但是当我试图在文本文件中写下它时,它什么也没显示。

 var img = JSON.parse(localStorage.getItem("iPath"));
                var img = JSON.stringify(img);
                console.log(img);

                $.ajax({
                    url: './php/temporary.php?deletefile',
                    cache: false,
                    type: 'POST',
                    data: img,
                    success: function( respond, textStatus, jqXHR ){

                        if( typeof respond.error === 'undefined' ){
                            //window.location.assign("/buyplace.html");
                        }
                        else{
                            console.log('ОШИБКИ ОТВЕТА сервера: ' +  respond.error );
                        }
                    },
                    error: function( jqXHR, textStatus, errorThrown ){
                        console.log('ОШИБКИ AJAX запроса: ' + textStatus );
                    }
                });

 if( isset( $_GET['deletefile'] ) ){
        $params = json_decode( $_POST);
        $myfile = fopen("testfile.txt", "w");
        fwrite($myfile, $params);
        //$img = "uploads/" . $imgPath;
        //move_uploaded_file($imgPath, "./uploads/");
        //unlink('./uploads/' . $img);
    }
    ?>

How can i solve this problem? 我怎么解决这个问题?

$_POST will contain key-value pairs and what you are sending, is a string. $_POST将包含键值对,并且您要发送的是一个字符串。

So you should either read the standard input, or you need to make sure that you are actually sending key-value pairs. 因此,您应该阅读标准输入,或者需要确保实际发送的是键值对。

The first case is already posted as a comment by @Scuzzy. @Scuzzy已将第一种情况作为评论发布。

For the latter, using the standard key-value pairs in $_POST : 对于后者,请在$_POST使用标准键值对:

 $.ajax({
      url: './php/temporary.php?deletefile',
      cache: false,
      type: 'POST',
      data: {json: img},
      // the rest of your js

And in php: 并在php中:

if( isset( $_GET['deletefile'] ) ){
    $params = json_decode($_POST['json']);
    // the rest of your php

There's no need to send the parameters as JSON. 无需将参数作为JSON发送。 You can use an object as the data: option, and each property will be sent as the corresponding $_POST element. 您可以将一个对象用作data:选项,每个属性将作为相应的$_POST元素发送。

var img = JSON.parse(localStorage.getItem("iPath"));
console.log(img);

$.ajax({
    url: './php/temporary.php?deletefile',
    cache: false,
    type: 'POST',
    data: img,
    success: function( respond, textStatus, jqXHR ){
        if( typeof respond.error === 'undefined' ){
            //window.location.assign("/buyplace.html");
        }
        else{
            console.log('ОШИБКИ ОТВЕТА сервера: ' +  respond.error );
        }
    },
    error: function( jqXHR, textStatus, errorThrown ){
        console.log('ОШИБКИ AJAX запроса: ' + textStatus );
    }
});

In PHP, you'll need to use json_encode() to convert the $_POST array to a string that can be written to a file. 在PHP中,您需要使用json_encode()$_POST数组转换为可以写入文件的字符串。

if( isset( $_GET['deletefile'] ) ){
    $params = $_POST;
    $myfile = fopen("testfile.txt", "w");
    fwrite($myfile, json_encode($params));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM