這是我第一次使用JSON。我想從使用ajax.But PHP腳本JSON數據我收到此錯誤「Error.Parsing JSON請求失敗」,「不確定」。幫助我,這是我的PHP腳本test.php的從php獲取json數據並在jquery中使用它
$data='{
"one": "One",
"two": "Two",
"three": "Three"
}';
header('Content-type: application/json');
echo json_encode($data);
,在這裏我得到的數據訪問getdata.php
var sURL = 'http://www.example.com/test.php';
$.ajax({
type: "GET",
url:sURL,
dataType:"jsonp",
crossDomain:true,
data: {transid:trans_id , bookingdate: dateVal, bookingtime: timeVal, People: peopleVal,affiliateid: affiliate },
async: false,
contentType:"application/json; charset=utf-8",
success: function (data){
var result = JSON.parse(data);
alert(result);
},
error: function (x, e) {
if (x.status == 0) {
alert(x.response);
alert(x + " " + e);
alert('You are offline!!\n Please Check Your Network.');
}
else if (x.status == 404) {
alert('Requested URL not found.');
}
else if (x.status == 500) {
alert('Internel Server Error.');
}
else if (e == 'parsererror') {
alert('Error.\nParsing JSON Request failed.' + e.statusText);
alert(x.response);
} else if (e == 'timeout') {
alert('Request Time out.');
} else {
alert('Unknow Error.\n' + x.responseText);
}
}
});
這是我的第一個問題,所以藉口任何錯誤
did not help.now火蟲也給了SyntaxError的錯誤:無效標籤 [Break On This Error] \t {「one」:「One」,「two」:「Two」,「three」:「Three」 }(第1行,第1列) –
@KaranTuteja:嘗試myanswer結合這個答案,它應該工作,因爲json_encode方法只接受數組.. ..! –
現在它在螢火蟲中給出這個錯誤SyntaxError:JSON.parse:意外字符 [Break On This Error] \t var result = JSON.parse(data); –