Heim > Artikel > Backend-Entwicklung > javascript - Backbone开发中遇到set的一个问题?
先上json的数据格式,底部有完整的php
<code>{ "id":0, "success": true, "user":{ "ready":"0", "name":"", //这里声明了name,没有声明tel "sex":"", "addr":[{"prov":"1省","city":["a","b","c"]}, {"prov":"2省","city":["aa","bb","cc"]}, {"prov":"3省","city":["aaa","bbb","ccc"]}], "birth":"1992-06-15", "wx":"" } } </code>
问用set方法创建tel的值?
<code>user=this.model.get('user'); user.tel=123; this.model.set('user',user) </code>
上面这个方法在实际用法无效 tel的值没有加进来
这里却又值
如果文件中存在一个属性,他是可以传值的;
<code>user=this.model.get('user'); user.name=123;//tel换成name this.model.set('user',user) </code>
访问的数据文件如下:
<code><?php if($_SERVER['REQUEST_METHOD']=="PUT"){ //echo json_encode(array("status"=>true)); parse_str(file_get_contents('php://input'),$put_vars); $put_vars=json_encode($put_vars); die($put_vars); die; } else{ echo '{ "id":0, "success": true, "user":{ "ready":"0", "name":"", "sex":"", "addr":[{"prov":"1省","city":["a","b","c"]}, {"prov":"2省","city":["aa","bb","cc"]}, {"prov":"3省","city":["aaa","bbb","ccc"]}], "birth":"1992-06-15", "wx":"" } }'; }</code>
先上json的数据格式,底部有完整的php
<code>{ "id":0, "success": true, "user":{ "ready":"0", "name":"", //这里声明了name,没有声明tel "sex":"", "addr":[{"prov":"1省","city":["a","b","c"]}, {"prov":"2省","city":["aa","bb","cc"]}, {"prov":"3省","city":["aaa","bbb","ccc"]}], "birth":"1992-06-15", "wx":"" } } </code>
问用set方法创建tel的值?
<code>user=this.model.get('user'); user.tel=123; this.model.set('user',user) </code>
上面这个方法在实际用法无效 tel的值没有加进来
这里却又值
如果文件中存在一个属性,他是可以传值的;
<code>user=this.model.get('user'); user.name=123;//tel换成name this.model.set('user',user) </code>
访问的数据文件如下:
<code><?php if($_SERVER['REQUEST_METHOD']=="PUT"){ //echo json_encode(array("status"=>true)); parse_str(file_get_contents('php://input'),$put_vars); $put_vars=json_encode($put_vars); die($put_vars); die; } else{ echo '{ "id":0, "success": true, "user":{ "ready":"0", "name":"", "sex":"", "addr":[{"prov":"1省","city":["a","b","c"]}, {"prov":"2省","city":["aa","bb","cc"]}, {"prov":"3省","city":["aaa","bbb","ccc"]}], "birth":"1992-06-15", "wx":"" } }'; }</code>
主要是由于model模型中不存在tel的值;所以无法被保存;所以要在模型的默认属性中先添加tel(空值也好,反正要声明)
你倒底想问什么??先把错别字改掉,把话讲通顺了。
你前端是修改了值然后传到后台吗?把前端的相关代码贴出来吧
既然POST到php的数据是有tel的,说么Model的数据已经修改了,去检查你php的代码,这和Backbone无关