PHP无法检查并从jquery获取json发送的数据:

时间:2014-09-09 19:55:43

标签: php jquery json post

当我尝试它时总是返回“错误”。我的代码在一个名为index.php的文件中 PHP:

if($_POST){
    $return="Success.";
    echo json_encode($return);
}

jQuery的:

function Save(){
                $.ajax({
                    url: "./index.php",
                    type: "POST",
                    data: { 
                        example:"example",
                        example2:$(".content").text()
                    },
                    dataType: "json"
                }).done(function(data){
                    $('#response').append('<p style="color:red;">'+data+'</p>');
                    alert("done");
                }).fail(function(){
                    $('#response').append('<p style="color:red;">Error</p>');
                });
            }

这是请求的网络选项卡,我认为POST一切正常。网络标签:

Remote Address:127.0.0.1:80
Request URL:http://localhost/test/test/test/index.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8,bg;q=0.6
Connection:keep-alive
Content-Length:606
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:_ga=GA1.1.1569762264.1406894118
Host:localhost
Origin:http://localhost
Referer:http://localhost/test/test/test/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
example:example
example2:
<?php
    if(true){
        echo 'ds';
        //das
    }
.test {
    size: 5pt;
}

Response Headersview source
Connection:Keep-Alive
Content-Length:1641
Content-Type:text/html
Date:Tue, 09 Sep 2014 20:03:46 GMT
Keep-Alive:timeout=5, max=97
Server:Apache/2.4.9 (Unix) OpenSSL/1.0.1g mod_fcgid/2.3.9 PHP/5.5.11 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By:PHP/5.5.11

以下是“网络”标签中的“响应”标签:

"Success."<!DOCTYPE html>
<html>
    <head>
        <title>Text</title>
        <script src="style/jquery.min.js"></script>
        <script>
        $(document).ready(function() {

            function Save(){
                $.ajax({
                    url: "./index.php",
                    type: "POST",
                    data: { 
                        filename:"something.php",
                        text:$(".content").text()
                    },
                    dataType: "json"
                }).done(function(data){
                    $('#response').append('<p style="color:red;">'+data+'</p>');
                    alert("done");
                }).fail(function(){
                    $('#response').append('<p style="color:red;">Error</p>');
                });
            }

            $(".content").focus(function(){
                $(window).bind('keydown', function(event) {
                    if (event.ctrlKey || event.metaKey) {
                        switch (String.fromCharCode(event.which).toLowerCase()) {
                            case 's':
                            event.preventDefault();
                            Save();
                            break;
                        }
                    }
                });
            });
        });
        </script>
        <style>
            .content {
                width: 80%;
                height: 80%;
                border: 1px solid #ccc;
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <div id="response"></div>
        <pre><div class="content">
&lt;?php
    if(true){
        echo 'ds';
        //das
    }
.test {
    size: 5pt;
}
        </div></pre>
    </body>
</html>

你能告诉我如何解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

如果客户端期望JSON,则无法打印除JSON之外的任何内容。之后你应该退出脚本:

if($_POST){
    $return="Success.";
    echo json_encode($return);
    exit();
}

?>
<!DOCTYPE html>
<html>
...

答案 1 :(得分:0)

$return = json_encode(array('success'));
echo $return;

答案 2 :(得分:0)

通过表单提交创建一个虚拟表单来发出POST请求,这样您就可以看到PHP脚本的实际输出。添加

ini_set('display_errors',1); 
error_reporting(E_ALL);

位于PHP页面的顶部,以显示PHP引发的任何错误。使用您的输入运行页面。修复任何错误,直到得到您期望的结果。然后返回带有JavaScript的页面,看看它是否正常工作。