php代码如下:<?php header('Content-Type: application/json'); header('Content-Type: text/html;charset=utf-8'); $email = $_GET['email']; $user = []; $conn = @mysql_connect("localhost","Test","123456") or die("Failed in connecting database"); mysql_select_db("Test",$conn); mysql_query("set names 'UTF-8'"); $query = "select * from UserInformation where email = '".$email."'"; $result = mysql_query($query); if (null == ($row = mysql_fetch_array($result))) { echo $_GET['callback']."(no such user)"; } else { $user['email'] = $email; $user['nickname'] = $row['nickname']; $user['portrait'] = $row['portrait']; echo $_GET['callback']."(".json_encode($user).")"; }?>js代码如下:<script> $.ajax({ url: "http://test.localhost/UserInterfaceForChatroom/UserInformation.php[email protected]", type: "GET", dataType: 'jsonp', // crossDomain: true, success: function (result) { // data = $.parseJSON(result); // alert(data.nickname); alert(result.nickname); } }); </script>其中遇到了两个问题:1、第一个问题:Uncaught SyntaxError: Unexpected token :解决方案如下:This has just happened to me, and the reason was none of the reasons above. I was using the jQuery command getJSON and adding callback=? to use JSONP (as I needed to go cross-domain), and returning the JSON code {"foo":"bar"} and getting the error.This is because I should have included the callback data, something like jQuery17209314005577471107_1335958194322({"foo":"bar"})Here is the PHP code I used to achieve this, which degrades if JSON (without a callback) is used:$ret['foo'] = "bar";finish();function finish() { header("content-type:application/json"); if ($_GET['callback']) { print $_GET['callback']."("; } print json_encode($GLOBALS['ret']); if ($_GET['callback']) { print ")"; } exit; }Hopefully that will help someone in the future.2、第二个问题:解析json数据。从上面的javascript中可以看到,我没有使用jquery.parseJSON()这些方法,开始使用这些方法,但是总是会报VM219:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1的错误,后来不用jquery.parseJSON()这个方法,反而一切正常。不知为何。以上这篇ajax调用返回php接口返回json数据的方法(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

代码还有待完善: 复制代码 代码如下:<script type="text/javascript">   // Close HTML Tags --------------------------------------------    function closeHTML(str){   var arrTags=["span","font","b","u","i","h1","h2","h3","h4","h5","h6","p","li","ul","table","div"];   for(var i=0;i<arrTags.length;i++){   var intOpen=0;   var intClose=0;   var  re=new RegExp("\\<"+arrTags[i]+"( [^\\<\\>]+|)\\>","ig");   var arrMatch=str.match(re);   if(arrMatch!=null) intOpen=arrMatch.length;   re=new RegExp("\\<\\/"+arrTags[i]+"\\>","ig");   arrMatch=str.match(re);   if(arrMatch!=null) intClose=arrMatch.length;   for(var j=0;j<intOpen-intClose;j++){   str+="</"+arrTags[i]+">";   }   /*for(var j=(intOpen-intClose-1);j>=0;j--){   str+="</"+arrTags[i]+">";   }*/   }   return str;   }   // Close UBB Tags --------------------------------------------   function closeUBB(str){   var arrTags=["code","quote","list","color","align","font","size","b"];   for(var i=0;i<arrTags.length;i++){   var intOpen=0;   var intClose=0;   var  re=new RegExp("\\["+arrTags[i]+"(=[^\\[\\]]+|)\\]","ig");   var arrMatch=str.match(re);   if(arrMatch!=null) intOpen=arrMatch.length;   re=new RegExp("\\[\\/"+arrTags[i]+"\\]","ig");   arrMatch=str.match(re);   if(arrMatch!=null) intClose=arrMatch.length;   for(var j=0;j<intOpen-intClose;j++){   str+="[/"+arrTags[i]+"]";   }   }   return str;   }   alert(closeHTML("正常:<div class='testBox'><ul><li><b>加粗</b><span>字体哈哈"));   alert(closeHTML("有错:

本文实例讲述了PHP模拟asp中response类的方法。分享给大家供大家参考。具体如下:习惯了asp或是asp.net开发的人, 他们会经常用到response类,这个类用于处理客户端的响应,可以实现跳转,输出等功能. 在php中没有这个类,但是确实可以通过函数来模拟这个类./* * 类用途: 实现类似于asp中的response功能 */final class Response { private $headers = array(); private $output; private $level = 0; public function addHeader($key, $value) { $this->headers[$key] = $value; } public function removeHeader($key) { if (isset($this->headers[$key])) { unset($this->headers[$key]); } } public function redirect($url) { header('Location: ' . $url); exit; } public function setOutput($output, $level = 0) { $this->output = $output; $this->level = $level; } private function compress($data, $level = 0) { if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)) { $encoding = 'gzip'; } if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE)) { $encoding = 'x-gzip'; } if (!isset($encoding)) { return $data; } if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) { return $data; } if (headers_sent()) { return $data; } if (connection_status()) { return $data; } $this->addHeader('Content-Encoding', $encoding); return gzencode($data, (int)$level); } public function output() { if ($this->level) { $ouput = $this->compress($this->output, $this->level); } else { $ouput = $this->output; } if (!headers_sent()) { foreach ($this->headers as $key => $value) { header($key . ': ' . $value); } } echo $ouput; } }希望本文所述对大家的php程序设计有所帮助。

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><form id="form1" name="form1" method="post" action="check_remember.php"><table width="300" border="1" align="center" cellpadding="0" cellspacing="0"><thead> <tr> <td colspan="2" align="center"><b>记住用户名和密码</b></td> </tr></thead> <tr align="center"> <td>用 户 名:</td> <td><input type="text" value="<?php echo $_COOKIE['name'];?>" name="name"></td> </tr> <tr align="center"> <td>密码:</td> <td><input type="password" name="password" value="<?php echo $_COOKIE['password']?>"></td> </tr> <tr align="center"> <td>记住用户名和密码</td> <td><?php if($_COOKIE['remember'] == 1){?><input type="checkbox" name="remember" value="1" checked><?php }else{($_COOKIE['remember'] == "")?><input type="checkbox" name="remember" value="1"><?php }?></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" name="Submit" value="提交" /></td> </tr></table></form> check_remember.php<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><?php $name = $_POST['name']; $password = $_POST['password']; $remember = $_POST['remember']; if($remember == 1){ setcookie('name',$name,time()+3600); setcookie('password',$password,time()+3600); setcookie('remember',$remember,time()+3600); }else{ setcookie('name',$name,time()-3600); setcookie('password',$password,time()-3600); setcookie('remember',$remember,time()-3600); } echo "<a href=\"remember.php\">返回</a>";?

本文实例详细讲述了php中socket的用法,分享给大家供大家参考。具体用法如下:一、开启socketphpinfo();查看是否开启了socket扩展,否则在php.ini中开启。二、服务器端代码的写法复制代码 代码如下:<?phperror_reporting(E_ALL);set_time_limit(0);//ob_implicit_flush();$address = '127.0.0.1';$port = 10005;//创建端口if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";}//绑定if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}//监听if (socket_listen($sock, 5) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}do { //得到一个链接 if (($msgsock = socket_accept($sock)) === false) {  echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";  break; } //welcome  发送到客户端 $msg = "<font color='red'>server send:welcome</font><br/>"; socket_write($msgsock, $msg, strlen($msg)); echo 'read client message\n'; $buf = socket_read($msgsock, 8192); $talkback = "received message:$buf\n"; echo $talkback; if (false === socket_write($msgsock, $talkback, strlen($talkback))) {  echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n"; } else {  echo 'send success'; } socket_close($msgsock);} while(true);//关闭socketsocket_close($sock);?>服务器端需要在cli模式是执行,有可能cli模式下php.ini文件载入的不一样可以像如下输出:这时候在zhoxh目录下就有个tem.text文件。查看 Configuration File (php.ini) Path => C:\WINDOWS 。不是我的php.ini 文件,这说明调用的php.ini文件时错误的。这时候我们要指定php.ini文件命令如下注意的是我的php可以直接执行时配置了环境变量。三、客户端复制代码 代码如下:<?php//error_reporting(E_ALL);echo "<h2>tcp/ip connection </h2>\n";$service_port = 10005;$address = '127.0.0.1';$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";} else { echo "OK. \n";}echo "Attempting to connect to '$address' on port '$service_port'...";$result = socket_connect($socket, $address, $service_port);if($result === false) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";} else { echo "OK \n";}$in = "HEAD / http/1.1\r\n";$in .= "HOST: localhost \r\n";$in .= "Connection: close\r\n\r\n";$out = "";echo "sending http head request ...";socket_write($socket, $in, strlen($in));echo  "OK\n";echo "Reading response:\n\n";while ($out = socket_read($socket, 8192)) { echo $out;}echo "closeing socket..";socket_close($socket);echo "ok .\n\n"; 执行结果如下: server:client:希望本文所述对大家的PHP程序设计有所帮助。

分类:腾博会官方网

时间:2016-10-11 10:21:13