Home > Backend Development > PHP Tutorial > javascript - js gets the return result of php

javascript - js gets the return result of php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:21:58
Original
2311 people have browsed it

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Reply content:

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

The value returned by php is not in json format, and js cannot parse it

echo json_encode()

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template