javascript - Uncaught SyntaxError: Unexpected token - JSON character encoding -


In my controller I handle post requests with AJAX and request directly that receives data from JS-code

mycontroller.php

$ sServiceNumber = isset ($ _ POST ['service number'])? $ _POST ['service number']: $ _GET ['service number']; // Here I call a db for sServiceNumber and get some data as array $ asAlrows $ aResponse ['ServiceNumber'] = $ sServiceNumber; $ AResponse ['data'] = $ alo; $ SJson = json_encode (auto :: Convert ToStrictUtf8 ($ aResponse)); If (isset ($ _ POST ['service number'])) {$ sJson echo; Go out; } And {$ sJs = '$ (document) .ready (function () {var sData = \' .. $ SJson. '\'; HandleResponseData (sData);}); '; MyHtmlHead :: addExtraJsCode ($ SJS); Continue presenting the // html page.}

When I call it with AJAX everything works fine, but when I handleResponseData () , I get it directly Uncaught syntax error:. Unexpected token on special characters

My javascript

  function handleResponseData (rawData) {response = jQuery.parseJSON (rawData); // & lt; - This is where the error occurs (on received requests) // Use the feedback} $ ("Form [name = 'search form']") Submit (function (event) {event.preventDefault (); // Send the post and get some data! $ .Post (world wide, {ServiceNumber: $ ("input [name = 'ServiceNumber']"). Val () , Time: "2pm"}) done (handleResponseData);});  

Last here comes my conversion method

  protected static function convertToStrictUtf8 ($ p_aValues) {function detectEncoding (& amp; $ mValue)! {If (mb_detect_encoding ($ MValue, 'UTF-8', true)) {$ mValue = utf8_encode ($ mValue); }} Array_walk_recursive ($ p_aValues, 'detect encoding'); Return $ p_aValues; }  

jquery $ How does Jason String recover when manually embedded in code when received from Post ? {"ServiceNumber": "485218-1138932068", "Data": [{"RowId": "AEEA-IU3A61" , "ThemeRequestId": "AEEA-IU39LX", "Name": "SV LIDEHÄLL"), "FILESIZE": "8812", "ServiceNumber": "485218-1138932068", "Subject": "O (BERGMAN / LIDEHÄLL) "," Area ":" svrT "," der ":" "485218-1138932068", "data": [{"roid": "1702"}}}

  {"service Number "AEEA-IU3A61", "ServiceRequestId": "AEEA-IU39LX", "Name": "SV LIDEH \ u00c4LL"), "FILESIZE": "8812", "ServiceNumber": "485218-1138932068", "Subject" : "O (Bergman \ / Lide \ u00c4LL) \ r \ n", "Area": ​​"svrT", "Dir": "1702"}}}  

The problem was caused by line break .

The docs says: "Control characters" such as tabs or new lines are not allowed.

When $ Post , the character of the new line was changed to the stud ring "\ r \ n" This is the reason that the matter was working.

Adding something to remove such new lines has finally resolved it.

  function stripNewLines (& amp; $ mValue) {if (is_string ($ mValue)) {$ mValue = trim (preg_replace ('/ \ s + /', '', $ mValue) ); }} Array_walk_recursive ($ aResponse, 'stripNewLines');  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -