<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 < data.length; i++) { source = data[i].fields; } } else { //$("#resultsHeader").html("No Results").show(); showErrorMessage("#error-container", "<strong>Ooops!</strong> 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>
The above js submission process.
The following is the code of 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>
Question: js can generate requests to php normally, but it cannot get the return value of php, whether it is $result or $key. What's going on?
Look at the js error report and you will get the following prompt
<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 < data.length; i++) { source = data[i].fields; } } else { //$("#resultsHeader").html("No Results").show(); showErrorMessage("#error-container", "<strong>Ooops!</strong> 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>
The above js submission process.
The following is the code of 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>
Question: js can generate requests to php normally, but it cannot get the return value of php, whether it is $result or $key. What's going on?
Look at the js error report and you will get the following prompt
<code>"VM2900:1 Uncaught SyntaxError: Unexpected token y in JSON at position 1" </code>
The value returned by php is not in json format, and js cannot parse it
echo json_encode()