JavaScript 中的一些字符转义

发布于 2024-12-09 17:25:14 字数 535 浏览 1 评论 0原文

我的旧应用程序中有一个 Ajax 调用:

i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId(
    "CRC:QueryStatus", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI
);

我想在 setTimeout 方法中添加此 Ajax 请求。为了逃避 " 我添加了 \。我想出了以下行:

setTimeout("i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId(\"CRC:QueryStatus\", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI)",50000);

现在我在控制台上没有收到任何错误,但 Ajax 调用也不起作用。

我错过了什么吗?

I have an ajax call in my legacy application:

i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId(
    "CRC:QueryStatus", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI
);

I want to add this Ajax request in setTimeout method. To escape " I added \. I came up with following line:

setTimeout("i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId(\"CRC:QueryStatus\", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI)",50000);

Now I am not getting any error on console but Ajax call is also not working either.

Am I missing anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2024-12-16 17:25:14

rec 和/或 scopedCallbackQRSI 变量可能是在本地范围内定义的(因此无法从全局范围访问)。当使用字符串化函数作为第一个参数调用 setTimeout 时,该函数将在 window 范围内执行。

要维护范围(并能够使用局部变量),请将代码包装在函数中,并将其作为第一个参数传递给 setTimeout

setTimeout(function(){
    i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId("CRC:QueryStatus", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI);
}, 50000);

The rec and/or scopedCallbackQRSI variables are probably defined in a local scope (thus not accessible from the global scope). When setTimeout is called with a stringified function as a first argument, the function is executed within the scope of window.

To maintain the scope (and be able to use the local variables), wrap your code in a function, and pass it as a first argument to setTimeout:

setTimeout(function(){
    i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId("CRC:QueryStatus", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI);
}, 50000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文