开发者

PHP won't return anyhting in response

I'm new to PHP and JQuery.

i Have this very simple PHP function

function save($data)
{
    $allData = load();
    array_push($allData, $data);
    global $filePath;
    $fp = fopen($filePath, 'w+') or die("I could not open $filePath.");
    fwri开发者_JAVA百科te($fp, serialize($allData));
    echo "{status:\"success\" , result:" . (string)(count($allData) - 1) . "}";
    fclose($fp);
}

which reads an array from the disk , aggregate data and returns a result. and i have this JQuery code :

  $jQuery.ajax({
            url: serverUrl,
            type: "GET",
            data: {method: "save", data: jQuery.param(pData)},
            cache: false,
            success: function (data) {
                alert("data");
            }
        });

now the thing is if i call the PHP method through a url in firefox , i get the result printed on screen right, if i call this method through the jQuery code above it will write to the file but won't return anything, and i see in firebug under "response" tab nothing. where did I go wrong?

Thanks.


You're alerting "data" instead of data onsuccess.

Try echoing properly formatted JSON:

echo '{"status": "success", "result": "'. (string)(count($allData) - 1) .'"}';

Then alert something like:

alert(data.status);


$jQuery.ajax({
            url: serverUrl,
            type: "GET",
            data: {method: "save", data: jQuery.param(pData)},
            dataType: 'json', //added by me
            cache: false,
            success: function (data) {
                alert("data");
            }
        });

and in your php use safer method

$var  = array('status' => 'success', 'result' => count($allData) - 1 );
echo json_encode($var);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜