Maison >développement back-end >tutoriel php >javascript - js获取php的返回结果问题
<code> $.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 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 + ' --<br>' + jso.error); } </code>
以上的js的提交过程。
以下是ajax.php的代码
<code> <?php require_once('bootstrap.php'); $url = ELASTICSEARCH_URL.'/test/_search'; $content = file_get_contents('php://input'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 5000); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($content))); $result= curl_exec($ch); $arr = json_decode($result); if ($arr->hits->total>0) { foreach ($arr->hits->hits as $es) { $source = $es->_source; foreach ($source as $key => $value) { echo "$key".":"."$value"; } } } ?> </code>
问题:js可以正常生成请求到php,但是取不到php的返回值,不管是 $result还是$key,请问这个怎么回事?
看js的报错,会得到如下的一个提示
<code>"VM2900:1 Uncaught SyntaxError: Unexpected token y in JSON at position 1" </code>
<code> $.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 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 + ' --<br>' + jso.error); } </code>
以上的js的提交过程。
以下是ajax.php的代码
<code> <?php require_once('bootstrap.php'); $url = ELASTICSEARCH_URL.'/test/_search'; $content = file_get_contents('php://input'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 5000); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($content))); $result= curl_exec($ch); $arr = json_decode($result); if ($arr->hits->total>0) { foreach ($arr->hits->hits as $es) { $source = $es->_source; foreach ($source as $key => $value) { echo "$key".":"."$value"; } } } ?> </code>
问题:js可以正常生成请求到php,但是取不到php的返回值,不管是 $result还是$key,请问这个怎么回事?
看js的报错,会得到如下的一个提示
<code>"VM2900:1 Uncaught SyntaxError: Unexpected token y in JSON at position 1" </code>
php返回的值不是json格式的,js没有办法解析
echo json_encode()