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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据