0

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);

?>
8
  • 'X-CMC_PRO_API_KEY: apikey', looks wrong, but it could just be your obfiscation Commented Apr 14, 2021 at 11:19
  • Have you checked what your PHP script is returning? If not, you can do so by opening the Network tab in the Developer toolbar and clicking on the AJAX request, then selecting Response in it. Commented Apr 14, 2021 at 11:21
  • @El_Vanja it shows the php file in plain text as a response Commented Apr 14, 2021 at 13:00
  • Are you sending this AJAX to the same page? Commented Apr 14, 2021 at 13:16
  • No, the ajax request is at popup.js and the url used for the ajax request is popup.php Commented Apr 14, 2021 at 13:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.