Heim >Backend-Entwicklung >PHP-Tutorial >PHPCMS通过getJSON调用的地址方法必须加public_PHP教程
今天遇到一个getJSON的问题,回调函数一直无法执行,检查了生成的json数据的格式绝对没问题,getJSON的js语法也没问题,但就是alert不出来传回的数据。原来是phpcms的check_hash()函数对远程调用的方法进行了安全验证,方法名前没public_的都不能通过。
<span /*</span><span * * 检查hash值,验证用户数据安全性 </span><span */</span> <span final</span> <span private</span> <span function</span><span check_hash() { </span><span if</span>(<span preg_match</span>('/^public_/', ROUTE_A) || ROUTE_M =='admin' && ROUTE_C =='index' || <span in_array</span>(ROUTE_A, <span array</span>('login'<span ))) { </span><span return</span> <span true</span><span ; } </span><span if</span>(<span isset</span>(<span $_GET</span>['pc_hash']) && <span $_SESSION</span>['pc_hash'] != '' && (<span $_SESSION</span>['pc_hash'] == <span $_GET</span>['pc_hash'<span ])) { </span><span return</span> <span true</span><span ; } </span><span elseif</span>(<span isset</span>(<span $_POST</span>['pc_hash']) && <span $_SESSION</span>['pc_hash'] != '' && (<span $_SESSION</span>['pc_hash'] == <span $_POST</span>['pc_hash'<span ])) { </span><span return</span> <span true</span><span ; } </span><span else</span><span { showmessage(L(</span>'hash_check_false'),<span HTTP_REFERER); } }</span>
phpcms给函数进行了hash验证,因此,现在方法是这样写的:
<span public</span> <span function</span> public_mobile_getjson_ids() {<span //</span><span publc是后来加上去的</span> <span $modelid</span> = <span intval</span>(<span $_GET</span>['modelid'<span ]); </span><span $id</span> = <span intval</span>(<span $_GET</span>['id'<span ]); </span><span $this</span>->db->set_model(<span $modelid</span><span ); </span><span $tablename</span> = <span $this</span>->db-><span table_name; </span><span $this</span>->db->table_name = <span $tablename</span>.'_data'<span ; </span><span $r</span> = <span $this</span>->db->get_one(<span array</span>('id'=><span $id</span>),'mobile_type'<span ); </span><span if</span>(<span $r</span>['mobile_type'<span ]) { </span><span $relation</span> = <span str_replace</span>('|', ',', <span $r</span>['mobile_type'<span ]); </span><span $relation</span> = <span trim</span>(<span $relation</span>,','<span ); </span><span $where</span> = "id IN(<span $relation</span>)"<span ; </span><span $infos</span> = <span array</span><span (); </span><span $this</span>->mobile_db = pc_base::load_model ( 'mobile_type_model'<span ); </span><span $datas</span> = <span $this</span>->mobile_db->select(<span $where</span>,'id,type_name'<span ); </span><span //</span><span $this->db->table_name = $tablename; //$datas = $this->db->select($where,'id,title');</span> <span foreach</span>(<span $datas</span> <span as</span> <span $_v</span><span ) { </span><span $_v</span>['sid'] = 'v'.<span $_v</span>['id'<span ]; </span><span if</span>(<span strtolower</span>(CHARSET)=='gbk') <span $_v</span>['type_name'] = <span iconv</span>('gbk', 'utf-8', <span $_v</span>['type_name'<span ]); </span><span $infos</span>[] = <span $_v</span><span ; } </span><span echo</span> json_encode(<span $infos</span><span ); } }</span>
js部分的getJSON是这样写的:
<span //</span><span 显示添加机型</span> <span function</span><span show_mobiletype(modelid,id) { $.getJSON(</span>"?m=content&c=content&a=public_mobile_getjson_ids&modelid="+modelid+"&id="+id, <span function</span><span (json){ </span><span var</span> newrelation_ids = ''<span ; </span><span if</span>(json==<span null</span><span ) { alert(</span>'没有添加相关文章'<span ); </span><span return</span> <span false</span><span ; } $.each(json, </span><span function</span><span (i, n){ newrelation_ids </span>+= "<li id='"+n.sid+"'>·<span>"+n.type_name+"</span><a href='javascript:;' class='close' onclick=\"remove_relation('"+n.sid+"',"+n.id+")\"></a></li>"<span ; }); $(</span>'#mobile_type_text'<span ).html(newrelation_ids); }); }</span>
就好了。要注意phpcms里面调用远程地址,方法前加上public啊!!