javascript 未捕获的 ReferenceError:foobar 未定义(匿名函数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8568332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:50:51  来源:igfitidea点击:

Uncaught ReferenceError:foobar is not defined (anonymous function)

javascriptjqueryajax

提问by Tauquir

I have this js file serving from some domain say foobar.com

我有这个来自某个域的 js 文件说 foobar.com

at http://foobar.com/static/js/main.js:

$(document).ready(function() {
        function foobar(bar){
            $.ajax({
                    url: "/site/foo/",
                data: {'foo':bar},
                    dataType: "jsonp",
                    crossdomain: !0,
                    success: function (data) {
                alert(data);
                    },
                    error: function () {
                    }
                })
        }   
    });

On barfoo.comon some url, I have something like this:

barfoo.com一些网址上,我有这样的事情:

<script src='http://foobar.com/static/js/main.js' type='text/javascript'></script>
<script type='text/javascript'>foobar('123456')</script>

When I hit that url : it says

当我点击那个网址时:它说

Uncaught ReferenceError:foobar is not defined (anonymous function)

How to access function from other domains?

如何从其他域访问功能?

回答by Pointy

You've defined "foobar()" inside the "ready" handler. It's therefore a local variable in that function, and invisible outside it.

您已经在“就绪”处理程序中定义了“foobar()”。因此,它是该函数中的一个局部变量,在它之外是不可见的。

You could add this to the end of the "ready" handler:

您可以将其添加到“就绪”处理程序的末尾:

  window['foobar'] = foobar;

and then it'd be visible globally.

然后它会在全球范围内可见。

By the way this is something that can bite at jsfiddlebecause it (by default) will wrap code in a "load" handler. Thus, if you copy/paste from a JavaScript file included in the <head>, a function that would be global in that context ends up not global in the fiddle.

顺便说一句,这可能会影响jsfiddle,因为它(默认情况下)会将代码包装在“加载”处理程序中。因此,如果您从包含在<head>.

回答by Steve O'Connor

Your function is not visible globally as pointed out by Pointy. But you are also loading a script and calling a function defined by the ready()function. I guess it's possible ready()may not have been called when your script calls foobar()which would generate the same error. I would recommend you set a global value in your script and then use that variable in the ready()function.

正如 Pointy 指出的那样,您的功能在全局范围内不可见。但是您也在加载脚本并调用由该ready()函数定义的函数。我想当ready()您的脚本调用foobar()会产生相同的错误时,可能没有被调用。我建议您在脚本中设置一个全局值,然后在ready()函数中使用该变量。

Set the value in the script:

在脚本中设置值:

<script src='http://foobar.com/static/js/main.js' type='text/javascript'></script>
<script type='text/javascript'>var bar = '123456'</script>

Then you can use it in the ready()function.

然后就可以在ready()函数中使用了。

$(document).ready(function() {
        $.ajax({
                url: "/site/foo/",
                data: {'foo':bar},
                dataType: "jsonp",
                crossdomain: !0,
                success: function (data) {
                   alert(data);
                },
                error: function () {
                }
            });  
       });

If you want to define a function so you can use it again you can call it in ready()but make sure you set the global variable barto what you want.

如果您想定义一个函数以便再次使用它,您可以调用它,ready()但请确保将全局变量设置为bar您想要的。

window.foobar = function() {
        $.ajax({
                url: "/site/foo/",
                data: {'foo':bar},
                dataType: "jsonp",
                crossdomain: !0,
                success: function (data) {
                   alert(data);
                },
                error: function () {
                }
            });
       }

$(document).ready(function() {
    foobar();
    // other stuff 
});