【问题标题】:jQuery $.post works in chrome, safari, but not FF (claims success callback function is undefined)jQuery $.post 适用于 chrome、safari,但不适用于 FF(声称成功回调函数未定义)
【发布时间】:2012-08-29 18:03:08
【问题描述】:

我有以下 jQuery 1.7.2 代码:

var theParams = encodeURIComponent(s1)+encodeURIComponent(s2);
$.post('/myURL',theParams,processData).error(errorResponse);
function processData(data,textStatus){
    blah blah;
}// end processData
function errorResponse() {
    blah blah;
}

此代码在 Safari 6 (Mac)、Chrome 21 (Mac)、Safari (iPad)、Chrome (iPad)、 但 FF 14 (Mac) 给了我以下错误:

ReferenceError: processData is not defined

奇怪的是,类似的代码(来自不同的页面)在 FF 上也能正常工作:

var formData = $(form).serialize();
$.post('/myURL',formData,processData).error(errorResponse);
function processData(data,textStatus) {
    blah blah;
}// end processData
function errorResponse() {
    blah blah;
}

我尝试重命名该函数,但这会导致相同的未定义错误。我应该寻找什么来调试这个?

【问题讨论】:

  • 原代码也适用于IE9(Windows 7)。
  • 您是否在 if 语句中定义函数?如果是这样,那就可以了:这在 JavaScript 规范中是不允许的,并且不同的浏览器实现它的方式不同。

标签: javascript jquery html firefox


【解决方案1】:

尝试重新排列您的代码

var processData, errorResponse, theParams;
processData = function (data,textStatus){
    //blah blah;
};
errorResponse = function () {
    //blah blah;
};
theParams = encodeURIComponent(s1)+encodeURIComponent(s2);
$.post('/myURL',theParams,processData).error(errorResponse);

这对你有用吗?

能否也请提供encodeURIComponent 功能?

【讨论】:

  • 这成功了!我所做的只是将 $.post 行移到其他两个下方。谢谢!
【解决方案2】:

尝试在第 1 行之后添加此行:

theParams = encodeURIComponent(theParams);

我假设你的 encodeURIComponent 函数是this one

【讨论】:

  • 我曾尝试删除 encodeURIComponent 但没有效果。请参阅 Saint Gerbil 的回复以进行修复:让 $.post 遵循 processData 和 errorResponse 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
  • 2023-04-05
  • 2017-11-25
  • 2011-05-24
  • 2013-06-30
  • 1970-01-01
  • 2015-08-11
相关资源
最近更新 更多