开发者

PHP return empty json string?

My html page sends an ajax request (POST) to PHP, PHP echo a json object.

Code has been tested and worked fine at my development view.

However, after they are uploaded to a VPS hosting server, the returned json string is empty (I used alert() at the response function to display the responseText and found out it was empty).

any idea about the problem?


I am new here. I have not figured out how to add a comment. (Please let me know.)

I changed the PHP code to return a string, it worked. so the problem is after json encoding.

new test.php (worked).

if(!isset($_POST['A'])||!isset($_POST['B']))
{
    echo "Failed";  
}
else
{
    echo "OK";  
}

HTML:

<html>
<head>
<title>Test Page</title>
<script type="text/javascript">

var NORMAL_STATE = 4;

function ajax_create_http()
{
    var xmlHttp = 0;
    try
    {
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {

        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {

            return 0;
        }
        }
    }

    return xmlHttp;
}

var http_req_inst = ajax_create_http();

function sendHttpRequest(params) 
{   
    if( !http_req_inst ) return;

    http_req_inst.open('POST', 'test.php', true);
    http_req_inst.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_req_inst.onreadystatechange = handleHttpResponse;
    http_req_inst.send(params);

    alert("Request sent");
}       

function handleHttpResponse()
{
    if( !http_req_inst ) return;

    if (http_req_inst.readyState == NORMAL_STATE) 
    {
        alert(http_req_inst.responseText);

    }
}

</开发者_开发知识库script>

</head>
<body>

    <div>
        <input type="button" value="click" onclick="sendHttpRequest('A=a&B=b')"/>
    </div>
</body>    
</html>

PHP:

<?php

if(!isset($_POST['A'])||!isset($_POST['B']))
{
    $rc = 1;
    $arr = array ('rc'=>(integer)$rc,'msg'=>'Testing failed.');
    echo json_encode($arr);     
}
else
{
    $rc = 0;
    $arr = array ('rc'=>(integer)$rc,'msg'=>'Testing passed.');
    echo json_encode($arr);     
}

?>

When I tested, the responseText is empty.


Just check the php configuration on both machines. Like on my machine $text will work on my provider I have to write like $_GET['text'] due to security settings. The PHP configuration will give you an idea of what's wrong, or what packages need to be installed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜