Uncaught reference error lift ajax is not defined

心已入冬 提交于 2019-12-19 10:54:09

问题


I'm trying to create a Lift chat server. I've taken everything straight from the Lift book that is linked to from Lift's main website. Upon running it, I cannot submit my messages because liftAjax is undefined and causes an error that appears in my chrome console.

Correct me if I'm wrong, but shouldn't Lift generate the liftAjax stuff upon starting up the website? I have a feeling I could import liftAjax myself from some source and it would work, but I don't feel I should have to do this.

This is my index.html file.

<!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><title>Home</title></head>
<body class="lift:content_id=main">
    <div id="main" class="lift:surround?with=default;at=content">
        <!-- the behavior of the div -->
        <div class="lift:comet?type=Chat">Some chat messages
            <ul>
                <li>A message</li>
                <li class="clearable">Another message</li>
                <li class="clearable">A third message</li>
            </ul>
        </div>
        <div>
            <form class="lift:form.ajax">
                <input class="lift:ChatIn" id="chat_in" />
                <input type="submit" value="Say Something" />
            </form>
        </div>
    </div>
  </body>
</html>

This is what is generated:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div id="main" xmlns="http://www.w3.org/1999/xhtml">
    <!-- the behavior of the div -->
    <div id="F156429460218VSI1GX_outer" style="display: inline">
        <div id="F156429460218VSI1GX" style="display: inline">
            <div xmlns="http://www.w3.org/1999/xhtml">Some chat messages
                <ul>
                    <li></li>
                </ul>
      </div><script type="text/javascript">
// <![CDATA[
/* JSON Func comet $$ F156429460221QEXVXK */function F156429460221QEXVXK(obj) {liftAjax.lift_ajaxHandler('F156429460221QEXVXK='+ encodeURIComponent(JSON.stringify(obj)), null,null);}
// ]]>
</script></div><script type="text/javascript">
// <![CDATA[
var destroy_F156429460218VSI1GX = function() {}
// ]]>
</script></div>

      <div>
        <form id="F156429460223BHVYIM" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+&quot;F156429460223BHVYIM&quot;).serialize(), null, null, &quot;javascript&quot;);return false;">

Note: the error happens on the above line

<input name="F1564294602245ECYAU" id="chat_in" xmlns="http://www.w3.org/1999/xhtml" />
<input value="Say Something" type="submit" xmlns="http://www.w3.org/1999/xhtml" />
</form>
</div>
</div> 

I'm just not really sure what could be causing this. I'm just starting to learn Lift so I do not know much. Am I missing something?

If you need more information, such as build.sbt or other files just let me know and I can post them as well.


回答1:


In my case, I was missing following body part that automatically seems to source liftAjax.

<body class="lift:content_id=main">
    <div id="main" class="lift:surround?with=default;at=content">
           ......
    </div>
</body>

But, in another case, sourcing jquery manually would work with JQuery module injected in Boot.scala.

<script id="jquery" src="/classpath/jquery.js" type="text/javascript"></script>



回答2:


You don't need to manually include the liftAjax function, but you do need to include jquery manually. Lift will not automatically inject a reference to it since it doesn't know what version, minification level, or location you want to use. Try adding:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

To the head section of your template.



来源:https://stackoverflow.com/questions/20747815/uncaught-reference-error-lift-ajax-is-not-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!