gpt4 book ai didi

jquery - jsonpCallback 未调用函数

转载 作者:行者123 更新时间:2023-12-01 06:19:38 24 4
gpt4 key购买 nike

这是我无法开始工作的 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/

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