简体   繁体   English

为什么 jquery 返回的值与提交的值不同?

[英]Why does jquery return different values than the values submitted?

Issue:问题:
I submit an object to jquery to convert into a serialized string that will become part of a "POST" request to a server, and the data returned from the serialization request is different than the data sent on many occasions.我将 object 提交给 jquery 以转换为序列化字符串,该字符串将成为对服务器的“POST”请求的一部分,并且从序列化请求返回的数据与多次发送的数据不同。

An example:一个例子:

The JavaScript code that implements the server POST request:实现服务器POST请求的JavaScript代码:

function send_data(gpg_data) {
    var query_string;
    query_string = '?' + $.param(gpg_data, traditional = true);
    console.log('gpg_data =', gpg_data) 
    console.log('query_string =', query_string);
    $.post(server_address + query_string);
    return;
}

This is the structure sent to the jquery param() function.这是发送到 jquery param() function 的结构。
(copied from the browser console in developer mode.) (从开发者模式下的浏览器控制台复制。)

gpg_data = 
{controller_status: 'Connected', motion_state: 'Stopped', angle_dir: 'Stopped', time_stamp: 21442, x_axis: 0, …}
angle_dir: "Stopped"
controller_status: "Connected"
force: 0
head_enable: 0
head_x_axis: 0
head_y_axis: 0
motion_state: "Stopped"
time_stamp: 21490
trigger_1: 0
trigger_2: 0
x_axis: 0
y_axis: "0.00"

. . . . . . and the returned "query string" was:并且返回的“查询字符串”是:

query_string = ?controller_status=Connected&motion_state=Stopped&angle_dir=Stopped&time_stamp=21282&x_axis=0&y_axis=0.00&head_x_axis=0&head_y_axis=0&force=0&trigger_1=1&trigger_2=1&head_enable=0

The data received by the server is:服务器接收到的数据为:

ImmutableMultiDict([('controller_status', 'Connected'), ('motion_state', 'Stopped'), ('angle_dir', 'Stopped'), ('time_stamp', '21282'), ('x_axis', '0'), ('y_axis', '0.00'), ('head_x_axis', '0'), ('head_y_axis', '0'), ('force', '0'), ('trigger_1', '1'), ('trigger_2', '1'), ('head_enable', '0')]) 

For example, note that "trigger_1" returns 1 when the data sent to it is a zero.例如,请注意“trigger_1”在发送给它的数据为零时返回 1。

I have tried setting the query to "traditional = true" to revert to an earlier style of query handling as some articles suggested - which did not work.我已尝试将查询设置为“traditional = true”,以恢复为某些文章建议的早期查询处理样式 - 这不起作用。 I tried this with jquery 3.2 and 3.6.我用 jquery 3.2 和 3.6 试过这个。

I am not sure exactly how jquery manages to munge the data so I have no idea where to look.我不确定 jquery 是如何管理数据的,所以我不知道去哪里找。

I have looked at my script and at the unpacked jquery code, and I can make no sense out of why or how it does what it does.我查看了我的脚本和解压缩的 jquery 代码,但我无法理解它为什么或如何做它的工作。

Any help understanding this would be appreciated.任何帮助理解这一点将不胜感激。

PS附言
web searches on "troubleshooting jquery" returned very complex replies that had more to do with editing e-commerce web pages with fancy buttons and logins than with simply serializing data. web 搜索“jquery 故障排除”返回了非常复杂的回复,这些回复与编辑电子商务 web 页面有关,而不是简单的序列化数据。

PPS聚苯乙烯
I am tempted to just chuck the jquery and write my own serialization routine.我很想放弃 jquery 并编写自己的序列化例程。 (grrrr!) (咕噜!)

=================== ====================

Update:更新:

As requested, a link to the browser-side context. 根据要求,链接到浏览器端上下文。

To run: unpack the zip file in a folder somewhere and attach an analog joystick/gamepad to any USB port, then launch index.html in a local browser.要运行:将 zip 文件解压缩到某个文件夹中,并将模拟操纵杆/游戏手柄连接到任何 USB 端口,然后在本地浏览器中启动 index.html。 Note that a purely digital gamepad - with buttons only or with a joystick that acts like four buttons - won't work.请注意,纯数字游戏手柄 - 仅带有按钮或带有像四个按钮一样的操纵杆 - 将无法工作。

You will want to try moving joystick axes 1 and 2, (programmatically axes 0 and 1) and use the first (0 th ) trigger button.您将想要尝试移动操纵杆轴 1 和 2(以编程方式为轴 0 和 1)并使用第一个 (0 th ) 触发按钮。

You will get a zillion CORS errors and it will complain bitterly that it cannot reach the server, but the server side context requires a GoPiGo-3 robot running GoPiGo O/S 3.0.1, so I did not include it.你会得到无数的 CORS 错误,它会抱怨它无法到达服务器,但是服务器端上下文需要一个运行 GoPiGo O/S 3.0.1 的 GoPiGo-3 机器人,所以我没有包括它。

Note: This does not work in Firefox as Firefox absolutely requires a "secure context" to use the Gamepad API.注意:这在 Firefox 中不起作用,因为 Firefox 绝对需要“安全上下文”才能使用游戏手柄 API。 It does work in the current version of Chrome, (Version 97.0.4692.99 (Official Build) (64-bit)), but throws warnings about requiring a secure context.它在 Chrome 的当前版本(版本 97.0.4692.99(官方构建)(64 位))中确实有效,但会引发有关需要安全上下文的警告。

Please also note that I have made every attempt I know how to try to troubleshoot the offending JavaScript, but trying to debug code that depends on real-time event handling in a browser is something I have not figured out how to do - despite continuous searching and efforts.另请注意,我已尽一切努力,我知道如何尝试解决有问题的 JavaScript,但尝试调试依赖于浏览器中实时事件处理的代码是我还没有弄清楚该怎么做的事情 - 尽管不断搜索和努力。 Any advice on how to do this would be appreciated!任何有关如何做到这一点的建议将不胜感激!

====================== =======================

Update:更新:
Researching debugging JavaScript in Chrome disclosed an interesting tidbit:在 Chrome 中研究调试 JavaScript 发现了一个有趣的花絮:

Including the line // @ts-check as the first line in the JavaScript code turns on additional "linting" (?) or other checks that, (mostly) were a question of adding "var" to the beginning of variable declarations.在 JavaScript 代码中将行// @ts-check作为第一行会打开额外的“linting”(?)或其他检查,(主要)是在变量声明的开头添加“var”的问题。

However.然而。 . . . . . .
There was one comment it made:它发表了一条评论:

gopigo3_joystick.x_axis = Number.parseFloat((jsdata.axes[0]).toFixed(2));
gopigo3_joystick.y_axis = Number.parseFloat(jsdata.axes[1]).toFixed(2);

I could not assign gopigo3_joystick.y_axis to a string object, (or something like that), and I was scratching my head - that was one of the pesky problems I was trying to solve!我无法将gopigo3_joystick.y_axis分配给字符串 object(或类似的东西),我摸不着头脑 - 这是我试图解决的讨厌问题之一!

If you look closely at that second line, you will notice I forgot a pair of parenthesis, and that second line should look like this:如果你仔细看第二行,你会注意到我忘记了一对括号,第二行应该是这样的:

gopigo3_joystick.y_axis = Number.parseFloat((jsdata.axes[1]).toFixed(2));

Problem solved - at least with respect to that problem.问题解决了——至少就那个问题而言。

I figured it out and I am banging my head in abject humiliation.我想通了,我在卑鄙的屈辱中敲着头。 . . . . And it had nothing to do with jquery.它与 jquery 无关。

Apparently two things are true, (aside from the fact that I am a blithering idiot):显然有两件事是真实的,(除了我是一个令人发指的白痴这一事实):

  1. The state of the gpg_data object's structure is "computed", (snapshot taken), the first time the JavaScript engine sees the structure and that is the state that is saved, (even though the value may change later on). gpg_data对象结构的 state 是“计算的”,(拍摄快照),JavaScript 引擎第一次看到该结构,就是 Z9ED39E2EA931586B73A985A6942 之后的值已保存。 In other words, that value is likely totally bogus.换句话说,这个值很可能完全是假的。

    Note:笔记:
    This may only be true for Chrome.这可能只适用于 Chrome。 Previous experiments with Firefox showed that these structures were updated each time they were encountered and the values seen in the console were valid.以前对 Firefox 的实验表明,每次遇到这些结构时都会更新,并且在控制台中看到的值是有效的。 Since Firefox now absolutely requires a secure context to use the gamepad API , I could not use Firefox for debugging.由于Firefox 现在绝对需要安全上下文才能使用游戏手柄 API ,因此我无法使用 Firefox 进行调试。

  2. I was trying to be "too clever".我试图“太聪明”。
    Given the following code snippet:给定以下代码片段:

function is_something_happening(old_time, gopigo3_joystick) {
    if (gopigo3_joystick.trigger_1 == 1 || gopigo3_joystick.head_enable == 1) {
        if (old_time != Number.parseFloat((gopigo3_joystick.time_stamp).toFixed(0))) {
            send_data(gopigo3_joystick)
            old_time = gopigo3_joystick.time_stamp
        }
    }
    return;
}

The idea behind this particular construction was to determine if "something interesting" is happening, where "something interesting" is defined as:这种特定构造背后的想法是确定“有趣的事情”是否正在发生,其中“有趣的事情”定义为:

  • A keypress, (handled separately)按键,(单独处理)
  • A joystick movement if either the primary trigger or the pinky trigger is pressed.按下主扳机或小扳机时的操纵杆运动。
    • Movement without any trigger pressed is ignored so that if the user accidentally bumps against the joystick, the robot doesn't go running around.没有按下任何扳机的运动将被忽略,因此如果用户不小心撞到操纵杆,机器人不会到处乱跑。

Therefore:所以:
The joystick data only gets updated if the trigger is pressed!仅当按下触发器时,操纵杆数据才会更新! In other words, trigger "release" events - the trigger is now = 0 - are not recorded!换句话说,触发“释放”事件 - 现在的触发 = 0 - 不被记录!
掌心

The combination of these two events - Chrome taking a "snapshot" of object variables once and once only, (or not keeping them current) - and the trigger value persisting, lead me to believe that jquery was the problem since the values appeared to be different on each side of the jquery call.这两个事件的组合——Chrome 一次又一次地获取 object 变量的“快照”,(或不保持它们当前)——并且触发值持续存在,让我相信 jquery 是问题,因为这些值似乎是jquery 调用的每一侧都不同。

Abject apologies to everyone involved.向所有相关人员深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM