我是JQuery,JSONP和AJAX的新手。我试图从外部文件中检索一些数据,但浏览器报告" ReferenceError:课程没有定义"从外部文件的开头行。我假设这就是为什么我的警报不会弹出,还是有其他问题?
外部文件的数据如下所示......
课程({"课程":[ {" id":" 1","开始
...等等...
我的代码如下(请注意,我已在此问题中替换了xx的url)...
<!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>
答案 0 :(得分:0)
正如@Salman指出的那样,我需要使用jsonpCallback: "courses"