1
我從PHP解析JSON的JavaScript解析JSON錯誤:語法錯誤:JSON.parse:位於第1行的JSON數據
當有問題的第2列意外的字符,這是我的示例代碼:
//function
MethodAjax = function (wsFile, param) {
return $.ajax({
type: "POST",
dataType: "json",
url: '../proses/' + wsFile + ".proses.php",
data: 'param='+param,
error: function (msg) {
return;
},
});
};
//call function
$(document).ready(function() {
$('#getproduk').click(function(){
var param = {
ProdukId : '1',
ProdukName : 'test'
};
CallMethodWithAjax('try', JSON.stringify(param)).done(function(data){
$data = JSON && JSON.parse(data) || $.parseJSON(data);
});
});
//Simple Php code
<?php
$data = $_POST['param'];
$data = (json_decode($data));
$data1['name'] = $data->ProdukName;
$data1['id'] = $data->ProdukId;
$data1['test'] = 'test';
echo json_encode($data1);
?>
//post, response and error at console
response : {"name":"test","id":"1","test":"test"}
post : param {"ProdukId":"1","ProdukName":"test"}
error : SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
如何解決這個probled,我有嘗試,我發現在SO和谷歌的解決方案,但仍解決不了這個問題
請別人幫
感謝
這項工作thaanks – Hans