$.ajax({
url: '/ajax.php',
type: 'POST',
contentType: 'application/json; charset=UTF-8',
crossDomain: true,
dataType: 'json',
data: JSON.stringify(data),
success: function(response) {
alert(response);
console.log(response);
$("#spinny").hide();
var data = response.hits.hits;
console.log(data);
var source = null;
if (data.length > 0) {
$("#resultsHeader").html(data.length + " Results").show();
for (var i = 0; i < data.length; i++) {
source = data[i].fields;
}
} else {
//$("#resultsHeader").html("No Results").show();
showErrorMessage("#error-container", "Ooops! No results found! Please try again.","alert-danger", true, 3000);
}
},
error: function(jqXHR, textStatus, errorThrown) {
var jso = jQuery.parseJSON(jqXHR.responseText);
error_note('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --
' + jso.error);
}
以上的js的提交过程。
以下是ajax.php的代码
hits->total>0) {
foreach ($arr->hits->hits as $es) {
$source = $es->_source;
foreach ($source as $key => $value) {
echo "$key".":"."$value";
}
}
}
?>
问题:js可以正常生成请求到php,但是取不到php的返回值,不管是 $result还是$key,请问这个怎么回事?
看js的报错,会得到如下的一个提示
"VM2900:1 Uncaught SyntaxError: Unexpected token y in JSON at position 1"
$.ajax({
url: '/ajax.php',
type: 'POST',
contentType: 'application/json; charset=UTF-8',
crossDomain: true,
dataType: 'json',
data: JSON.stringify(data),
success: function(response) {
alert(response);
console.log(response);
$("#spinny").hide();
var data = response.hits.hits;
console.log(data);
var source = null;
if (data.length > 0) {
$("#resultsHeader").html(data.length + " Results").show();
for (var i = 0; i < data.length; i++) {
source = data[i].fields;
}
} else {
//$("#resultsHeader").html("No Results").show();
showErrorMessage("#error-container", "Ooops! No results found! Please try again.","alert-danger", true, 3000);
}
},
error: function(jqXHR, textStatus, errorThrown) {
var jso = jQuery.parseJSON(jqXHR.responseText);
error_note('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --
' + jso.error);
}
以上的js的提交过程。
以下是ajax.php的代码
hits->total>0) {
foreach ($arr->hits->hits as $es) {
$source = $es->_source;
foreach ($source as $key => $value) {
echo "$key".":"."$value";
}
}
}
?>
问题:js可以正常生成请求到php,但是取不到php的返回值,不管是 $result还是$key,请问这个怎么回事?
看js的报错,会得到如下的一个提示
"VM2900:1 Uncaught SyntaxError: Unexpected token y in JSON at position 1"
php返回的值不是json格式的,js没有办法解析
echo json_encode()