dtcyv3985 2014-05-10 19:24
浏览 56
已采纳

使用AJAX / PHP上传和下载变量

I have a jQuery script file on a page which requires data to be uploaded. Depending on if the data uploaded has a value or not, the PHP page will either return the current time (if value uploaded !isset()), or some sql data. However, when the variable I upload actually has a value, it still returns the !isset() method. Am I doing something incorrectly?

AJAX

    $.ajax({
        url: 'download.php',
        type: 'REQUEST',
        datatype: 'json',
        data: ({
            last_downloaded: latestTimestamp,
            username: username
        }),
        success: function (data) {
        parsedData = $.parseJSON(data);

            if (!latestTimestamp) {
                latestTimestamp = parsedData.most_recent;
            }
        }
    });

}

if latestTimestamp is null, (it should be the first time this method is run), then the most recent time is run. However when it runs the second time, latestTimestamp has a value when I console.log.

PHP

<?php

// Get variables sent
$last_chat_time = $_REQUEST['last_downloaded'];
$username = $_REQUEST['username'];

// Start echoing JSON

if (!isset($last_chat_time)) {
    // User did not send last chat time they have, assume they just joined
    // Get the most recent chat date
    $SQL     = 'SELECT current_timestamp() as "most_recent" from dual';
    $results = mysql_fetch_assoc(mysql_query($SQL));
    $last_chat_time = $results;
    echo '{"most_recent":"' . $results['most_recent'] . '"}';
} 
else{
    $SQL = 'return some tasty data'
    $result = mysql_query($SQL);

    $messages = Array();

while ( $row = mysql_fetch_assoc($result) ) {
    $messages[] = Array(
        'chat' => $row['chat'],
        'time' => $row['time_sent'],
        'username' => $row['username']
    );
}

echo json_encode($messages);

}

On the php, it ALWAYS returns the first if. However, if I visit the url directly for the php page and append ?last_downloaded=somedate, it returns the correct information. Am I doing the AJAX incorrectly?

  • 写回答

1条回答 默认 最新

  • doushi1974 2014-05-10 19:31
    关注

    To me this has to be updated to type : 'post or get' because php's $_REQUEST handles both, so you can change your type to this:

    $.ajax({
        url: 'download.php',
        type: 'post',
    

    or this:

    $.ajax({
        url: 'download.php',
        type: 'get',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档