1

I am using ajax to send parameters and consult them, everything is functional only when obtaining the array with the records does not allow me to display the values independently

this is index.php

<script>
    $(document).ready(function(){
        $("#Montox").change(function(){
            var varmonto = $("#Montox").val();
            $.ajax({
                method: "post",
                url: "ajax/calc.php",
                data: {monto:varmonto}
            })
            .done(function(data){

                    $('#chattext').html(data['username1']);
                    $('#chattext2').html(data['username2']);/*I NEED TO SHOW THE DATA 'username2'*/

            });
        });
    });
</script>

this is my calc.php

<?php
echo json_encode(array('username1' => "luis", 'username2' => "jose"));

?>
2
  • The code should work. What is it doing instead? Commented Sep 2, 2019 at 21:05
  • It is not working, it does not show me the value of username1 and username2 Commented Sep 2, 2019 at 21:07

4 Answers 4

3

You're not parsing the response as JSON. You can:

  1. Use the dataType: 'json' option to $.ajax() to make it parse it as JSON automatically.
  2. Call header("Content-type: text/json"); in PHP to tell jQuery that the response is JSON.
  3. Use data = JSON.parse(data) in the .done() function to parse it explicitly.
Sign up to request clarification or add additional context in comments.

1 Comment

JSON.parse can trown an error and stuck your code when the JSON is malformed. If the ajax header or the dataType is set as JSON, the ajax "fail()" promise or "error" callback will be triggered if the JSON is malformed.
2

Since you're expecting a JSON result from your server, you need to set the dataType property on your ajax call to return a Javascript object that you can manipulate. Like this:

<script>
    $(document).ready(function(){
        $("#Montox").change(function(){
            var varmonto = $("#Montox").val();
            $.ajax({
                method: "post",
                url: "ajax/calc.php",
                data: {monto:varmonto},
                dataType: 'json'
            })
            .done(function(data){
                    // 'data' is a javascript object, not an array!

                    $('#chattext').html(data.username1);
                    $('#chattext2').html(data.username2);/*I NEED TO SHOW THE DATA 'username2'*/

            });
        });
    });
</script>

That should work.

Comments

1

Force JSON object inside the ajax with:

$.ajax({
     method: "post",
     url: "ajax/calc.php",
     data: {monto:varmonto},
     dataType: "json" //<--- HERE
})

Comments

1

You need add Json TYPE in your ajax header dataType: "json" it will allow you to get json text and parse it.

And for the next time, post the RESULT of (data) if you wanna a real help

Comments

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.