gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:39:23 26 4
gpt4 key购买 nike

我的页面上有一个 jQuery 脚本文件,需要上传数据。根据上传的数据是否有值,PHP 页面将返回当前时间(如果上传了值!isset()),或者返回一些 sql 数据。但是,当我上传的变量实际有值时,它仍然返回 !isset() 方法。我做错了什么吗?

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;
}
}
});

}

如果latestTimestamp为null,(应该是该方法第一次运行),则运行最近一次。但是,当它第二次运行时,latestTimestamp 在我 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);

}

在 php 上,它总是返回第一个 if。但是,如果我直接访问 php 页面的 url 并附加 ?last_downloaded=somedate,它将返回正确的信息。我是否错误地执行了 AJAX?

最佳答案

对我来说,这必须更新为 type : 'post or get' 因为 php 的 $_REQUEST 可以处理这两种情况,因此您可以将类型更改为:

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

或者这个:

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

关于javascript - 使用 AJAX/PHP 上传和下载变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com