简体   繁体   English

如何将数组正确转换为json_encode

[英]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 演示:jQuery Ajax使用JSON Return调用PHP脚本

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.... 我不明白为什么我的数组不能在json中变换....

These code are in response.php : 这些代码在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 : 在我的index.html中的部分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"); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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