我正在寫一個php腳本,需要將json變量加載到js變量中作爲json。首先我創建數組,然後使用json_encode將它變成一個字符串。然後我運行一些js,以便在客戶端訪問它。像這樣:firefox抱怨有效的JSON ...無形的無效字符?
$sBaseData = Filters::$sFilterBaseTypeData;
$sBaseData = str_replace('\r',' ',$sBaseData);
$sBaseData = str_replace('\n',' ',$sBaseData);
$sBaseData = str_replace('\t',' ',$sBaseData);
echo <<<HTML
<script type="text/javascript">
var validation_data = JSON.parse('$sBaseData');
</script>
HTML;
火狐抱怨意想不到的人物在這裏:
var validation_data = JSON.parse('{"enumeration":{"js":"","msg":""},"date":{"js":" var parts = widget.value.split('-'); var d = new Date(parts[0],parts[1],parts[2]); PASS = (d.getDay()>=1); ","msg":"Invalid date. Please Enter a date in the format: YYYY-MM-DD"},"text":{"js":"","msg":"what did you do?"},"integer":{"js":"if (isNaN(widget.value)) { PASS = false; } else { intVal = parseInt(widget.value); PASS = (widget.value == intVal); } ","msg":"Please enter an integer value"},"decimal":{"js":"PASS = isNaN(widget.value); ","msg":"Please enter a number"},"group":{"js":"","msg":""},"dealer":{"js":"","msg":""}}')
我試着用http://jsonlint.com/找出哪些角色是有過錯的,但它說,它的所有有效和精彩。我替換了一些導致問題的角色,還有什麼我應該替換?
這是一個壞主意,認爲該數據被更改,除非你可以肯定它不會輕易更換。 –
這是沒有錯的,最好是編碼。 – mmg666