简体   繁体   中英

Error with JavaScript Ajax Query and Smarty

I am trying to use an AJAX JSONP Query to read documents from a CouchDB I have hosted on Cloudant.

This is webpage. The following is relevant code section.

<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>

However, when I put this script tag into that webpage, I just get a blank webpage and I don't know why. I'm not even calling the function and JSLint says that there is no syntax error.

Template HTML File

{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}

There is no version of $.ajax() that supports a second argument as a callback.. ( that syntax is just for the shorthand versions like $.get() , $.post() , $.getJSON .. )

Also note that you are using jquery methods, and if you have not loaded jquery before this snippet, it will stop javascript execution if $ is not defined.

Use

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

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