MVVM架构~knockoutjs系列之为Ajax传递Ko数组对象

返回目录

一些要说的

这是一个很有意思的题目,在KO里,有对象和数组对象两种,但这两种对象对外表现都是一个function,如果希望得到他的值,需要进行函数式调用,如ko_a(),它的结果为一个具体值或者数组或者函数,而ko_a则表示一个KO对象。

今天主要说一下如何在ajax方法里向后台传递对象和数组等参数,一般说来,我们为后台传递的是字符串和数字,而如果希望传递对象,则需要使用$.param这个方法,下面具体用代码说一下。

一些要做的

一般后以的代码理想是这样的

   public JsonResult GeneratorOrder(int[] idArr)//一些需要被产生订单的购物车记录ID
{
//TODO:Generator Order
return Json(idArr);
}

而这样的后台接口来说,我们前台ajax的参数需要做一个$.param的处理,如下

         $.ajax({
type: "post",
url: "/order/GeneratorOrder",
data: $.param( self.SelCart(),true), //! 把数组直接作为$.param的参数是不可以的
dataType: "json",
success: function (data) {
Boxy.alert("To payPage of products:" + JSON.stringify(data));
}
});

而事实上,上面的代码运行结果却是错误的,因为$.param方法只支持对象,不支持直接的数组,而数组如果在对象里,是可以被支持的,这也很有意思,所以,上面代码我们需要修改一下

         $.ajax({
type: "post",
url: "/order/GeneratorOrder",
data: $.param({ idArr: self.SelCart() },true), //! 注意要传递第二个参数
dataType: "json",
success: function (data) {
Boxy.alert("To payPage of products:" + JSON.stringify(data));
}
});

下面的运行结果是我们希望看到的

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVQAAABTCAIAAABPgmjuAAAL10lEQVR4nO2dTU8bSRqA319DuFsY+T9wsP+DD8Y5tMQlmfyDHHyCeMXBCk6kHLnsJIA1iiJn1/lgFSBE6o2RZ3bIOgkznpCMMxNYD/Yeqru6Pt5qdwPdjd3vIytp11dXVdfTVV0GAzZBEKkEugRBpBKSnyBSCslPECmF5CeIlELyE1edDx8+JF2F6YTkJ646JH9ETIL8qwsAAAur0ZQ8s/QYC7mUk/JComtCROg9EzzfORrqezqS/zysnwEMK61ut9ttVYYAo0yl74Y7x7j8P2y3bz94or9+2G4jqdkF53gXUY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4E8iYuL88d6k20pMEvCNiJg45U+6rcQkQfJHDslPXE3ghIiYOOVPuq3EJAGnRMTEKX/SbSUmCfhfYKL42umUEJv8BBEcGATGjmsQEwQRA/BXYEh+gpgmSH6CSClwFhiSnyCmCRgGhuQniGmC5CeIlALBk5L8BDFNkPwEkVJIfoJIKSQ/QaSUS5Tf/bLZMX9XxoD7RRbmb6kiCOIyuVT5axcW9zLKIAgiCJHK76sy/5M14kqB5CeIuIhM/jF/eLJTsxznm5b09ZUkP0HEQ6Qzf9PyeYLvdDpONovkJ4j4SU5+N5Ml7g+S/AQRF4nKr38zPclPEHERl/ydWk7ZAGhabkCzySNIfoKIi4g3/ITNfEO0vN1P8hNEXMT0Qz7e35c3QT/kQxDxQj/eSxApheQniJRC8hNESiH5CSKlkPwEkVJIfoJIKSQ/QaQUkp8gUgrJTxApheQniJQSTn6CIKaGEPITBDFNkPwEkVJIfoJIKSQ/QaQUkp8gUgrJTxAp5f9LpKdSlajU1QAAAABJRU5ErkJggg==" alt="" />

返回目录

上一篇:【elasticsearch】(3)centos7 安装中文分词插件elasticsearch-analyzer-ik


下一篇:探究linux文件