简体   繁体   中英

'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 :". 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? Do i need the content-type somewhere in there? The two zipcodes are hard coded into the url.

In the first line you have ' $(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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