2017-06-25 81 views
0

我正在致力于javascript/vue.js页面at this link。我将此代码添加到上面的文件:

$(document).ready(function() { 
    myIP(); 
}); 

function myIP() { 
    $.getJSON("//freegeoip.net/json/?callback=?", function(data) { 
    // after succesful ajax response call myCallback with data.ip 
    myCallback(data.ip); 
    }); 
} 

function myCallback(theIP) { 
// do your stuff here 
    console.log("Data.ip:", theIP); 
    configDefault.lex.sessionAttributes.ip = theIP; 
    //console.log("ConfigDefault:", configDefault); 
}; 

当我加载网页我看$ sign undefined错误。然后我加入<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>但后来我开始错误

Uncaught Error: Module build failed: SyntaxError: Unexpected token (34:0) 

    32 | 
    33 | 
> 34 | <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    |^
    35 | 
    36 | // TODO turn this into a class 

有一件事我注意到,有在上面的链接没有<head>部分的文件中,最上SO提到所示的解决方案就摆在<head> jQuery的链接部分。

我需要做些什么来解决这个问题?

回答

1

您链接至的文件是JavaScript文件,扩展名为.js。 JavaScript文件没有<head>部分,这些文件用于HTML文件,扩展名为.html。你会需要找到JavaScript文件链接到HTML文件,并添加:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

到的是,在它的头部。 jQuery将在JavaScript文件中工作。