简体   繁体   English

“未捕获到的SyntaxError:意外令牌:”

[英]'Uncaught SyntaxError: Unexpected token :'

 $(onReady); function onReady() { console.log('jq'); $('#submitButton').on('click', calcDistance); } function calcDistance() { console.log('button clicked'); $.ajax({ type:'GET', dataType: 'jsonp', contentType: "application/json", url:"https://www.zipcodeapi.com/rest/WvKkFrPlOUywte1GcfPJVGmhqHjTLwQH1B2H2R3QMCmfQoV2DLMzr8QRQ9KBGBiI/distance.json/55044/55124/mile/", header: { "Access-Control-Allow-Origin": "*" }, success: function(res) { console.log( 'back from ther server with',res ); }, error: function( err ){ console.log( err ); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script src="client.js" charset="utf-8"></script> </head> <body> <h1>Zipcode</h1> <!-- <input id='firstZipcode' type="text" name="" value="First Zipcode"> <input id='secondZipcode' type="text" name="" value="Second Zipcode"> --> <button id='submitButton' type="button" name="button">Enter</button> </body> </html> 

I am making a call to the zipcode api and i am getting the error "Uncaught SyntaxError: Unexpected token :". 我正在对邮政编码api进行调用,并且收到错误"Uncaught SyntaxError: Unexpected token :". I have no idea where that is coming from. 我不知道那是哪里来的。 I am totally lost on how i would fix this. 我完全不知道如何解决这个问题。 Is it in the header when i make the ajax call? 我进行ajax调用时,它在标题中吗? Do i need the content-type somewhere in there? 我是否需要在那里的内容类型? The two zipcodes are hard coded into the url. 这两个邮政编码被硬编码到URL中。

In the first line you have ' $(onReady); 第一行中有' $(onReady); '. ”。 Try 尝试

jQuery(document).ready(function($){
..put all your other code here.
}

From what I can gather from your posted code it's the '$' that's not recognised, as it hasn't been bound to jQuery. 根据我从您发布的代码中收集到的信息,由于尚未绑定到jQuery,因此无法识别的是“ $”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM