AJAX Return ParseError with plain text of php file

98 Views Asked by At

I am doing an ajax request to a php file to return a certain value. I did some console logs to check the results:

readyState: 4
Status: 200
responseText: php file in plain text
error: parseError

Can anyone help me?

request:

$.ajax({
    type: "get",
    dataType: 'json',
    url: "popup.php" ,
    success : function(data) { 
        $("#stock").html("€" + data);
    },
    error:function(xhr,err){
        console.log(err);
        console.log(xhr.responseText)
    }
});

php file:

<?php
    $array = array();
    $url = 'url_name';
    $parameters = [
        'start' => '4',
        'limit' => '1',
        'convert' => 'EUR'
    ];

    $headers = [
        'Accepts: application/json',
        'X-CMC_PRO_API_KEY: apikey',
        'Content=Type: application/json',
    ];
    $qs = http_build_query($parameters);
    $request = "{$url}?{$qs}";

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => $request,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_RETURNTRANSFER => 1
    ));

    $response = curl_exec($curl); 
    curl_close($curl);
    $response_data = json_decode($response);    
    $res_data = $response_data->value;

    echo json_encode($res_data);

?>
0

There are 0 best solutions below