Home >Backend Development >PHP Tutorial >The address method called by PHPCMS through getJSON must be added public_PHP tutorial
I encountered a problem with getJSON today. The callback function has been unable to be executed. I checked the format of the generated json data and there is absolutely no problem. There is no problem with the js syntax of getJSON, but the alert cannot be sent back. data. It turns out that the check_hash() function of phpcms performs security verification on remote calling methods. Method names without public_ in front of them cannot pass .
<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 hashed the function, so the method is now written like this:
<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>
The getJSON in the js part is written like this:
<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>
That’s it. Please note that when calling the remote address in phpcms, add public before the method! !