0

well I know it must look like a beginner question and I do hope I've missed something easy but I can't seem t find what...

I am trying to get a JSON type back but I only get the html code in return.

Here is my code, I'm not really a beginner but I am far from being a pro so I tried some testing. If i put dataType it throws me an error, even though now my code looks like this :

$("#search_ref").click(function(){
    var ref_article = $("#ref_article").val();
    $("#search_buttons").append('<button id="new_search" type="button" class="btn btn-warning"><span class="glyphicon glyphicon-search"></span> Nouvelle recherche</button>');
    $("#search_ref").remove();
    $(".construct_delete").remove();
    nb_elements = 1;
    size = 20;
    $.ajax({
        url : 'search_ref.php',
        type : 'POST',
        data : 'ref_article=' + ref_article,
        success : function(response){
            alert(response);
            console.log(response);
            var table_construct = JSON.parse(response);
            alert(table_construct);
        },
        error : function(){
            alert("Error durant \"search_ref\"");
        }
    });
})

And the other part:

<? 

$banane = [1, 2, 3, 4 ];
$banane = json_encode($banane);
echo $banane;

?>

This is not my final code but I don't get why this isn't working, how can I force the JSON data instead of the html ?

Here is what I tried : dataType:JSON, setting the second php page doctype to JSON ?,

The message of the alert is this now:


( ! ) Parse error: syntax error, unexpected '$banane' (T_VARIABLE) in C:\wamp64\www\StageNeyret\search_ref.php on line 5

Dustman0
  • 29
  • 7
  • 2
    What do you mean "I get html code in return"? What does the response look like? – bool3max Jul 02 '18 at 18:48
  • 1
    The php code you have shown should construct valid json and return it. If it is not, then you are leaving out something as part of your *"This is not my final code"* that is important. – Taplar Jul 02 '18 at 18:53
  • @Taplar this is the code I'm testing, right now it is only that – Dustman0 Jul 02 '18 at 19:07
  • Add `dataType: 'json',` to your ajax options. What exactly do you receive in the 'raw response data' as seen in your browsers developer network tab? – IncredibleHat Jul 02 '18 at 19:07
  • @Dustman0 then we need to see exactly what the response is on the request that is getting the error when parsed. – Taplar Jul 02 '18 at 19:09
  • 1
    "*The message of the alert is this*" ... so, your server isn't even parsing the php file as php. Theres the problem. Maybe you forgot ` – IncredibleHat Jul 02 '18 at 19:11
  • I just needed to add php after – Dustman0 Jul 02 '18 at 19:39
  • @IncredibleHat I took this code back from my internship, I am the one who made it and I didn't need to add php there. – Dustman0 Jul 02 '18 at 19:41
  • If a server was jury-rigged in some way to understand that only ` ` meant php ... then that was how you had it work before. There is a server setting to accept the shorthand `= ` ... but thats only if the server is configured for it (not normally default setup). As for the new parse error... the code you show us has no such error. Not reproducible. Good luck! – IncredibleHat Jul 02 '18 at 20:40

0 Answers0