0

我正在尝试使用 AJAX JSONP 查询从我在 Cloudant 上托管的 CouchDB 读取文档。

是网页。以下是相关代码部分。

<script type="text/javascript">
var url = "https://acharya.cloudant.com/toxtweet";
console.log(url);
function getCloudantData(url) {
        $.ajax({
          'url': url, 
          'dataType': 'jsonp'
        }, 
        function (data) { 
          $('#importeddata').text(data);
        });
}
</script>

但是,当我将此脚本标签放入该网页时,我只是得到一个空白网页,我不知道为什么。我什至没有调用该函数,JSLint 说没有语法错误。

模板 HTML 文件

{extends file='toxtweet.tpl'}
{block name="head"}
   <link href="./styles/poll.css" rel="stylesheet" type="text/css" media="Screen">
   <script type="text/javascript">
           var url = "https://acharya.cloudant.com/toxtweet";
           console.log(url);
       $(function(){
          $.ajax({
           'url': 'https://acharya.cloudant.com/toxtweet', 
           'dataType': 'jsonp', 
            success:function (data) { 
             $('#ing').text(data); 
          }});});
     </script>
 {/block}
 {block name="body"}
 <h2> Do you think this tweet is discussing drugs? </h2>
     <form class="poll" method="post" actions="">
     <p id="chosen-tweet"><p>
     <ul>
    {foreach $answers as $answer}
        <li>
            <label class="poll_active">
                <input type="radio" name={$answer} value="0">{$answer@key}
            </label>
        </li>
    {/foreach}
</ul>
</form>
<div id="ing"></div>
 {/block}
 {debug}
4

1 回答 1

3

没有版本$.ajax()支持第二个参数作为回调..(该语法仅适用于简写版本,如$.get(), $.post(), $.getJSON..

$另请注意,您正在使用 jquery 方法,如果您在此代码段之前没有加载 jquery,如果未定义,它将停止 javascript 执行。

利用

<script type="text/javascript">
function getCloudantData(url) {
    $.ajax({
        'url': 'https://acharya.cloudant.com/toxtweet', 
        'dataType': 'jsonp', 
        success:function (data) { 
            $('#importeddata').text(data); 
        }
    });
}
</script>
于 2012-12-22T14:48:53.350 回答