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 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包