这是我第一次使用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); –