2014-10-28 96 views
1

在我的控制器中我處理與AJAX POST請求,並直接從GET請求到JS代碼未捕獲的SyntaxError:意外的標記 - JSON字符編碼

mycontroller.php

$sServiceNumber = isset($_POST['ServiceNumber']) ? $_POST['ServiceNumber'] : $_GET['ServiceNumber']; 

//Here I do a db call for sServiceNumber and fetch some data as array $aAllrows 

$aResponse['ServiceNumber'] = $sServiceNumber; 
$aResponse['Data'] = $aAllrows; 
$sJson = json_encode(self::convertToStrictUtf8($aResponse)); 

if (isset($_POST['ServiceNumber'])) { 
    echo $sJson; 
    exit; 
} else { 
    $sJs = ' 
    $(document).ready(function() { 
     var sData = \'' . $sJson . '\'; 
     handleResponseData(sData); 
    });'; 

    MyHtmlHead::addExtraJsCode($sJs); 

    //continue with rendering html page here.. 
} 

當我打電話添加數據這與阿賈克斯一切工作正常,但當我試圖直接運行handleResponseData()我得到Uncaught SyntaxError: Unexpected token特殊字符。

我的JavaScript

function handleResponseData (rawData) { 
    response = jQuery.parseJSON(rawData); //<--this is where the error occurs (on GET requests) 

    //Make use of response data 
} 

$("form[name='searchform']").submit(function(event) { 
    event.preventDefault(); 
    // Send post and fetch some data! 
    $.post(baseUrl, { ServiceNumber: $("input[name='ServiceNumber']").val(), time: "2pm" }).done(handleResponseData); 
}); 

最後來了我的轉換方法

protected static function convertToStrictUtf8 ($p_aValues) { 
    function detectEncoding(&$mValue) { 
     if (!mb_detect_encoding($mValue, 'UTF-8', true)) { 
      $mValue = utf8_encode($mValue); 
     } 
    } 
    array_walk_recursive($p_aValues, 'detectEncoding'); 

    return $p_aValues; 
} 

如何來獲取時使用jQuery $.post但不是當手動嵌入代碼的JSON字符串解析罰款?我該如何解決這個問題?

編輯: 從console.log(rawData)

精簡RAWDATA的版本GET

{"ServiceNumber":"485218-1138932068","Data":[{"RowId":"AEEA-IU3A61","ServiceRequestId":"AEEA-IU39LX","Name":"SV LIDEHÄLL)","FileSize":"8812","ServiceNumber":"485218-1138932068","Subject":"O(BERGMAN/LIDEHÄLL) 
","Area":"svrT","Dir":"1702"}]} 

POST

{"ServiceNumber":"485218-1138932068","Data":[{"RowId":"AEEA-IU3A61","ServiceRequestId":"AEEA-IU39LX","Name":"SV LIDEH\u00c4LL)","FileSize":"8812","ServiceNumber":"485218-1138932068","Subject":"O(BERGMAN\/LIDEH\u00c4LL)\r\n","Area":"svrT","Dir":"1702"}]} 
+0

發佈原始數據。有一個解析錯誤 – Timmetje 2014-10-28 09:30:39

+0

更新後。是的,有一個解析錯誤。但爲什麼? – daker 2014-10-28 09:45:26

+0

正如你所看到的,你的json數據不同。 – Timmetje 2014-10-28 09:50:44

回答

1

問題是由Line breaks引起的。

由於jQuery.parseJSON()文檔指出:「控制字符」,如製表符或換行符是不允許的。

當用$.post呼叫時,新的換行字符被轉換爲字符串"\r\n",這就是爲什麼該情況正在起作用。

添加這樣的東西來刪除新行終於解決了它。

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

在你的PHP控制器,回聲JSON respose鍵入以下行之前

header("Content-Type: application/json"); 

以及在jQuery的AJAX調用您需要提及的數據類型爲JSON

$.ajax({ 
..., 
..., 
dataType: 'JSON', // it is necessary to get JSON response 
... 
}); 
+0

'dataType:'JSON','只需要服務器是關於內容類型(哪個'header(「Content-Type:application/json」);'fixes)。 – Quentin 2014-10-28 10:16:20

+0

這不是問題。另外,如果你閱讀這個問題,那麼$ .ajax調用可以正常工作。 – daker 2014-10-28 11:09:15

相關問題