简体   繁体   中英

How to transform correctly an array to json_encode

I found this and its very helpful...

Demo: jQuery Ajax Call to PHP Script with JSON Return

But i have some problem with to use it....

I need to understand how to use it correctly...

I dont understand why my array cannot be transform in a json....

These code are in response.php :

$champs = ["user" => "", "combinaison" => 0, "valeurSmall" => "", "valeurBig" => "", "aucune_valeur" => false, "trop_valeur" => false, "color_red" => 0, "color_green" => 0, "color_blue" => 0];

$return["json"] = json_encode($champs, JSON_FORCE_OBJECT); 
echo json_encode($return); 

And in my index.html the part javascript :

$.ajax({
    type: "POST",
    dataType: "json",
    url: "changerMise/changer.php", 
    data: data,
    success: function(dataReturn) {                    
    alert("Form submitted successfully.\nReturned json: " + dataReturn["json"] );
       }
   });

The alert dont come back with the thing i need to use after....

The result gave :

Form submitted successfully.
Returned json: {"aucune_valeur": true}

any ideas ?!??!

Thx guys

你是否在php文件的顶部给出了json的响应输出,如下所示?

<?php header("Content-type: application/json; charset=utf-8"); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM