gpt4 book ai didi

javascript - Ajax 无法获得成功功能。总是出错的函数。无法将数据发送到另一个文件

转载 作者:行者123 更新时间:2023-12-01 01:27:31 26 4
gpt4 key购买 nike

请耐心等待。据我所知,这是我的第一篇文章。

这是我的 calendar.js 脚本的一部分。我正在尝试将从 index.php 中的模态窗口获取的数据POSTsql.php

function saveModal() { /*JQuery*/
var current_date_range = $(".modal-select#daterange").val();
var current_room_number = $("#mod-current-room-number").val();
var current_room_state = $("#mod-current-room-state").val();
var myData = {"post_date_range": current_date_range, "post_room_number": current_room_number, "post_room_state": current_room_state};
var myJSON = JSON.stringify(myData);

$.ajax({
type: "POST",
url: "sql.php",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
data: myJSON,
beforeSend: function() {
$("#ajax").html("<div class='loading'><img src='/images/loader.gif' alt='Loading...' /></div>");
},
success: function(result){
$("#ajax").empty();
$("#ajax").html(result);
$("#ajax").fadeIn("slow");
window.location.reload(true);
},
error: function(){
alert(myJSON);
$("#ajax").html("<p class='error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Oops!</strong> Try that again in a few moments.</p>");
}
})
}

我得到的数据很好(正如你所看到的,我已经用 alert(myJSON); checkin 了 error: function() )。它看起来像这样:{"post_date_range":"12/19/2018 - 12/28/2018","post_room_number":"118","post_room_state":"3"}。没关系daterangepicker.js返回可怕的 MM/DD/YYYY 格式的日期,我非常想将其更改为 YYYY-MM-DD。真正的问题是,代码永远不会成功:function()。

现在我的 sql.phpcalendar.jsindex.php 位于同一文件夹中。

sql.php 中,我尝试使用以下方法检索这些值:

$currentDateRange = $_REQUEST['post_date_range'];
$currentRoomNumber = intval($_REQUEST['post_room_number']);
$currentRoomState = intval($_REQUEST['post_room_state']);

我检查了许多其他 SO 问答,但没有一个能帮助我解决我的问题。我没有看到任何拼写错误。它并不违反同源策略规则。我不想使用 jQuery $.post 函数。有人看到明显的解决方案吗?

最佳答案

您想在 post 中发送数组而不是字符串,因此直接发送 myData 以获取 PHP 文件中的数组值,而不是转换为 JSON 字符串。它将根据您的需要与您当前的 PHP 文件配合使用。

关于javascript - Ajax 无法获得成功功能。总是出错的函数。无法将数据发送到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53627069/

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