A fairytale called 'PHP and the 'ufeff' character' › KnijnOps

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?