A fairytale called 'PHP and the 'ufeff' character' › KnijnOps
文章推薦指數: 80 %
A fairytale called 'PHP and the 'ufeff' character' › Afairytalecalled‘PHPandthe‘\ufeff’character’NottoolongagoIbuiltanAPIforoneofmycustomers.Theyneededavery(read:very)basicresponsefromtheirserversidescripttotheclient. TheAPIwasbuiltinlessthanhalfanhour(usingexistingtechnologiesandsecuritybest-practicesthatwerealreadyinplace).AndsoIstartedsendingsomerequeststotheendpointwithPostman,andgodItrulylovePostman…Anyways,IfoundthattheresponseIgotlookedlikethetypeofresponseIwouldexpectandcoulduse.ItwasasimpleJSONobject,with'some'nestedvariables.Nothingscary,likeIsaid. ThenIstartedmakingtherequestsfromtheclientside.IusedJquery'sown$.ajaxtomaketherequestsandIobviouslytoggledtheresponsebetween.errorand.success.ThecodeThecodewassimpleandlookedlikethis: $.ajax({ url:'MyEndPointURL?getMyItems=1', type:"GET", dataType:'json', success:function(items,textStatus,jqXHR){ //Dosomethingwiththedata $.each(items,function(index,item){ Console.log(item); }); }, error:function(jqXHR,textStatus,errorThrown){ console.log(errorThrown); } }); Andthebackendlookedlikethis: require_once('../includes/SomeController.php'); if(isset($_GET["getMyItems"])) { //GettheobjectswithamethoddeclaredinSomeController.php $myObjectArray=GetMyObjects(); echojson_encode($myObjectArray); } TheissueAsIsaid;theresponseIsawinPostmanlookedvalidtome.Ibasicallygotalistofxitemsallwithvalidvaluesandwiththeresponseheader'application/json'.However,Ididnotreceiveconsolelog'sprintingoutitems.Instead;Igotthiserror:Weird,sincetheNetworkpreviewtabinChromeclearlyshowedallmyitemswithvalidvalues: Afterlotsandlotsofcode-checking,Googlingandrage(andIadmit;IalmostsendthewholearrayofobjectsatexttobeparsedbyJS'sJSON.Parsejusttofindoutitstillcouldn'tbeparsed)IstumbleduponalittlereddotinfrontoftheresultintheresponsetabofthenetworktabinChrome:Becauseitdoesnot'break'thepreviewitdidn'toccurtometheJSONthatwasreturnedmighthavebeeninvalid.Well;Ithoughtwrong.Thislittlereddot,asIfoundout,wasthe'\ufeff'character.Acharacterthatiscalledthe'ByteOrderMark'(BOM),moreinfofoundhere.BecauseIdidinclude(orrequired)aclasstobeusedinmyAPIbackenditapparentlyaddedtheBOMinfrontofmyresponse.PHPseesthisasablanklinewhenreturninganycontentfromapageandthusaddedtheBOM.IwasunabletofindanyfilethatwasincludedinmyAPIbackendthatusedtheBOMcharacter,oranythingalike.ThesolutionIresolvedthisissuebyusingPHP'sfunctionob_clean().Ob_Cleanclearsthecontentsoftheoutputbuffer.ThereforeIcalledthismethodbeforeIstartedmyoutputfromtheAPI.Theissueresolvedimmediately,andPHPlivedhappilyeverafter(without\ufeff). LeaveacommentCancelreply Savemyname,email,andwebsiteinthisbrowserforthenexttimeIcomment. Submit ThissiteusesAkismettoreducespam.Learnhowyourcommentdataisprocessed. RecentPosts AutomatingShareGatemigrations AIForEveryone–Thenon-techcoursebyDeeplearning.ai Afairytalecalled‘PHPandthe‘\ufeff’character’ CognitiveServices–AddingknowledgetoyourbotwithQnAMaker AzureCognitivesServices–HowtobuildasimplecontextualBot Categories ArtificialIntelligence Programming Uncategorized TweetsbyknijnOps Copyright2019KnijnOps© AllRightsReservedTerms&Conditions | Privacy Thiswebsiteusescookiestoimproveyourexperience.We'llassumeyou'reokwiththis,butyoucanopt-outifyouwish.CookiesettingsACCEPTPrivacy&CookiesPolicy Close PrivacyOverview Thiswebsiteusescookiestoimproveyourexperiencewhileyounavigatethroughthewebsite.Outofthesecookies,thecookiesthatarecategorizedasnecessaryarestoredonyourbrowserastheyareessentialfortheworkingofbasicfunctionalitiesofthewebsite.Wealsousethird-partycookiesthathelpusanalyzeandunderstandhowyouusethiswebsite.Thesecookieswillbestoredinyourbrowseronlywithyourconsent.Youalsohavetheoptiontoopt-outofthesecookies.Butoptingoutofsomeofthesecookiesmayhaveaneffectonyourbrowsingexperience. Necessary Necessary AlwaysEnabled Necessarycookiesareabsolutelyessentialforthewebsitetofunctionproperly.Thiscategoryonlyincludescookiesthatensuresbasicfunctionalitiesandsecurityfeaturesofthewebsite.Thesecookiesdonotstoreanypersonalinformation. Non-necessary Non-necessary Anycookiesthatmaynotbeparticularlynecessaryforthewebsitetofunctionandisusedspecificallytocollectuserpersonaldataviaanalytics,ads,otherembeddedcontentsaretermedasnon-necessarycookies.Itismandatorytoprocureuserconsentpriortorunningthesecookiesonyourwebsite. SAVE&ACCEPT
延伸文章資訊
- 1PHP解决\ufeff问题_倒立蹬三轮的博客
写项目是请求接口发现返回的json数据无法用json_decode进行转换。查看Network发现返回值前面有个小红点(\ufeff)众所周知文件编码是含BOM的会导致 ...
- 2PHP中出现BOM字符\ufeff,PHP去掉诡异的BOM \ufeff_zpjing
PHP中出现BOM字符\ufeff,PHP去掉诡异的BOM \ufeff · <? · header('content-Type: text/html; charset=utf-8'); · i...
- 3PHP中出現BOM字元\ufeff,PHP去掉詭異的BOM \ufeff - 程式人生
滑鼠移上去,提示“\ufeff”。 百度下,發現是BOM字元。 前幾天還好好的,難道是因為今天替換某些內容,導致的? 從網上找了個php程式,掃描所有的 ...
- 4php響應Json字串頭部出現非法字元「\ufeff」的問題處理
php響應Json字串頭部出現非法字元「\ufeff」的問題處理. 2020-07-16 10:05:35. 1 出現問題,響應json前面有個小紅點,滑鼠放上去就會顯示ufeff ...
- 5php - Can't remove \ufeff from a string - Stack Overflow
php - Removing the "\ufeff" from the end of object -> content in ...