ホームページ >バックエンド開発 >PHPチュートリアル >phpがjsonステートメントを形成するとき、その前に余分な文がありますが、それを削除するにはどうすればよいですか?
php ソースコード
<?php //session start session_start(); include "./mobile/sql.php"; $con = mysql_connect($domain,$usrnm,$password); mysql_select_db($usrnm) or die('Could not select database'); mysql_query("set character set 'utf8'"); $page = $_GET['page']; if(!$con){ echo("can not connect"); } else{ //1.登陆授权 if($page=='login'){ //1.读取用户数据,并进行匹配 $usrname = $_GET['usrnm']; $pas = $_GET['pas']; //2.检索数据库 $sql = "select * from mb_usr"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); while( $row = mysql_fetch_array($result) ){ if(($row['usrid']==$usrname || $row['mail']==$usrname || $row['phoneno']==$usrname) && $row['pas']==$pas){ //3.分配session值 保留登陆时限 $_SESSION[$usrname] = $usrname; //4.构建login的json格式 $jsonArr = Array('logintype'=>$row['usrtype'],'sessionid'=>$usrname,'usrnm'=>$row['usrnm'],'usrid'=>$row['usrid']); echo json_encode($jsonArr); break; } else{ echo '{"logintype":"-1"}'; } } } }?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">{"logintype":"0","sessionid":"0","usrnm":"mjc","usrid":"0"}
該当するプログラムファイルをBOMなしのutf8ファイルに転送するだけでOKです。
最善の方法は、新しい BOM フリー ファイルを作成し、そこにコードをコピーして保存することです。
この文を PHP ページから削除してください
Add
header('Content-type: application/json');
試してみてください~
PHP ページからこの文を削除してください
こんな奇妙な問題に遭遇したのは初めてです、笑
どうやって削除しましたか? これは私にも起こりました
HTTP/1.1 200 OK
日付: Fri, 18 Apr 2014 09:00:59 GMT
サーバー: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
"BuyPriceoff":0,"BuyTime":"Undetermined","CityId":420100,"CityName":"武漢","Content":"White New Year's Eve","CountyId":420105,"CountyName" : "漢陽","CreateTime":"2014-02-06 11:35:27","DealerId":11569,"HandleTime":"2014-01-07 11:35:28","Id":23010786 、 "名前":"張国雄","注文タイプ":0,"電話番号":"18108624658","省ID":420000,"省名":"湖北省","ReDealerId":11569,"シリーズId":2945, " SeriesName":"Citroen C4L","SpecId":15737,"SpecName":"Citroen C4L 2013 1.8L Manual Jinzhi Edition","State":1},{"BuyPriceoff":0,"BuyTime": "", "CityId":140100,"CityName":"太原","Content":"","CountyId":140105,"CountyName":"小店","CreateTime":"2014-02-06 11 :34: 35","DealerId":6752,"HandleTime":"2014-01-07 11:34:36","Id":23010726,"Name":"李","OrderType":1,"Phone": "15386885719","ProvinceId":140000,"ProvinceName":"Shanxi","ReDealerId":6752,"SeriesId":792,"SeriesName":"Citroen C5","SpecId":17043,"SpecName ":"シトロエン C5 2013 2.0L オートマチック エンジョイタイプ","状態":1},{"購入価格オフ":0,"購入時間":"","CityId":440300,"CityName":"深セン", "Content":" ","CountyId":0,"CountyName":"","CreateTime":"2014-01-07 11:34:30","DealerId":75882,"HandleTime":"", "Id":23010715 ,"Name":"李","OrderType":0,"Phone":"18320819196","ProvinceId":440000,"ProvinceName":"Guangdong","ReDealerId":0,"SeriesId ":2945," SeriesName":"Citroen C4L","SpecId":17658,"SpecName":"Citroen C4L 2014 1.8L オートマチック インテリジェント ドライブ バージョン Jinzhi","State":0},{"BuyPriceoff" :0,"BuyTime":" ","CityId":320300,"CityName":"徐州","Content":"","CountyId":0,"CountyName":"","CreateTime":"2014 -01-07 11:34: 04","DealerId":5248,"HandleTime":"","Id":23010688,"Name":"武","OrderType":0,"Phone":"13852043028 ","ProvinceId":320000, "ProvinceName":"江蘇省","ReDealerId":0,"SeriesId":2945,"SeriesName":"シトロエン C4L","SpecId":15739,"SpecName":"シトロエン C4L 2013 1.8L オートマチック ジンチ エディション", "State":0},{"BuyPriceoff":0,"BuyTime":"","CityId":440300,"CityName":"Shenzhen","Content":"Platinum Grey","CountyId":0, "CountyName":"","CreateTime":"2014-02-06 11:33:39","DealerId":9802,"HandleTime":"2014-01-07 11:33:40","Id" :23010669,"Name":"Pan","OrderType":0,"Phone":"13923846836","ProvinceId":440000,"ProvinceName":"Guangdong","ReDealerId":9802,"SeriesId":2945 ,"シリーズ名":"シトロエン C4L","仕様ID":14551,"スペック名":"シトロエン C4L 2013 1.6L マニュアル 晋志版","状態":1}] 、"ページ":40337、"合計":201683 },"キャッシュ":1000,"メッセージ":"","成功":1,"結果":0}NULL