4

I am trying to use a javascript function defined outside of the $(document).ready(function(){}); as the callback for a $.get() request. However, firebug shows:

ReferenceError: temp is not defined
    $.get('twitter.php', function(data){temp(data)});

Here's the relevant code:

<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="txt/javascript">       
    function temp(data){
        alert(data);
    }
</script>
<script>
    $(document).ready(function() {
        $.get('twitter.php', function(data){temp(data)});
    });
</script>

twitter.php does return data.

4

4 回答 4

3

Remove type="txt/javascript", or change it to text/javascript.

于 2012-11-19T03:38:52.037 回答
2

 function temp(data){
    alert(data);
 }

以上

$(document).ready(function(){

});
于 2012-11-19T03:44:00.087 回答
1

You have a small syntax error which causes the script to be invalid, type="txt/javascript" should be:

<script type="text/javascript">       
 function temp(data){
    alert(data);
 }
</script>
于 2012-11-19T03:38:52.787 回答
1

您将脚本类型设置为'txt/javascript',如果我没记错的话,应该是'text/javascript',您的函数后还需要一个分号。

<script type="text/javascript">       
    function temp(data){
        alert(data);
    };
</script>
于 2012-11-19T03:39:45.993 回答