简体   繁体   中英

ReferenceError: XXX is not defined (JSONP, JQUERY, AJAX)

I am brand new to JQuery, JSONP and AJAX. I am attempting to retrieve some data from an external file, but the browser reports "ReferenceError: courses is not defined" from the opening line of the external file. I'm assuming this is why my alert doesn't pop up, or is there some other problem?

The external file's data begins as follows ...

courses({ "courses": [ { "id": "1", "start

... etc etc ...

And my code is as follows (note, I have replaced the url for xx in this question) ...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Request json test</title> 
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script src="json-jquery.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
  $.ajax({
    url: 'http://xx.xx.xx.xx/getCourses.php?action=getUnpaid',
    dataType: 'jsonp',
    success: function( data ) {
      alert("Success!");               
    }
  });
});
</script>

</head>
<body>
<a href="#" id="getdata-button">Get JSON Data</a>
<div id="showdata"></div>
</body>
</html> 

正如@Salman指出的那样,我需要使用jsonpCallback: "courses"

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