这是我无法开始工作的 jquery 片段。我试图通过 jsonpCallback 调用 blackBackground()
$(document).ready(function()
{
function blackBackground(data, status)
{
console.log("black background");
//I want to eventually change the body style to black
}
$.ajax({
url: 'http://localhost:3000/someurl',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'blackBackground'
});
});
更新:
jsonpCallback: 'blackBackground'
至
jsonpCallback: blackBackground
同时将 blackBackground 移至全局范围。感谢大家的回复。
最佳答案
这里的问题是函数 blackBackground
在全局范围内不可用。
您可以通过如下声明来在全局范围内公开该函数:
window.blackFunction = function(){ .. }
...或者在ajax配置中使用匿名函数:
jsonpCallback: function(result){ .. }
我推荐后者,因为它会让你的全局范围保持整洁的时间更长一点:)
关于jquery - jsonpCallback 未调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15347201/