douganmo1121 2013-06-17 11:36
浏览 81
已采纳

从AJAX调用中加载knock中的knockout.js形式

I'm attempting to load some HTML stored as text for a knockout.js form. A customer's server will use an AJAX GET call to my server which will then return the HTML for the table. Right now, the table is being returned, however it's formatted like knockout.js hasn't been loaded but I'm not receiving any errors (dropdown not populated, foreach loop for a table is displaying a blank row, a secondary div is displayed when it should only be displayed once a submit button has been pressed).

When I have the HTML on this page it works, so I'm pretty sure it's not incorrect code in the form. The only thing I can think of is that knockout is picky about single and double quotes, although I feel like that would rsult in errors. Right now I'm testing on my server so same origin policy shouldn't be an issue at the moment.

Here is the page that initially loads

//main.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>

<!-- I also tried to include the knockout file includes here but made no difference -->

<script>
<!-- call to get HTML for knockout form -->
$.ajax({
        type: 'GET',
        url: '/orders/return_form',
        data: "true",
        success: function(response) { // on success..
            $('#order_div').html(response); // update the DIV
        }
    })
</script>

<div id = "order_div">
        <!-- returned knockout form goes here -->
</div>


<script src="knockout.js"></script>
<script src="knockout_info.js"></script>  <!-- MVVM file -->
<script src = "Knockout-Validation/Src/knockout.validation"</script>

Here is the code to return the code for the table

//orders/return_form

if($_GET['true']){

     echo "table code here ";  
     //I tried to encode this before but got all kinds of extra markup due to whitespace and other misc. characters

    //or

    $form = new stdClass;
    $form->table = "<form method='post'>
                    <input type='textbox' name='text'/>
                    <input type='submit' name='textsubmit'/>
                </form>";
    echo $form->table;


}

If I include the table instead of calling for the code it works. However, it's a pretty lengthy form and considering this code will be on someone else's server calling to mine, I'd like to keep their code to a minimum.

  • 写回答

1条回答 默认 最新

  • dongpo2014 2013-06-17 11:38
    关注

    You're not doing any binding.

    success: function(response) { // on success..
        $('#order_div').html(response); // update the DIV
        ko.applyBindings(myModel); // rebind
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程