3

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

1
  • echo json_encode($champs,JSON_FORCE_OBJECT); ... Commented Aug 13, 2018 at 2:36

1 Answer 1

0

Are you has give header json for response output on top of your php file, like below ?

<?php header("Content-type: application/json; charset=utf-8"); ?>
Sign up to request clarification or add additional context in comments.

4 Comments

If you using POST method you must know where data name/value will be executed/inspected. $_POST["json"] example, there data from inputed form will be executed/testing.
You must add header json, and add allow cors origins
Yes. Ajax json must be reading json object (not html). I think your PHP returned html header, not json.
I know why i didnt receive any information at my output responce...At the end of one of my function, i forgot this ««««return $champs;»»» Please don't throw any rotten tomatoes lol

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.