-1

This is my functions.php script containing the following code. The array is encoded into JSON.

functions.php

 $final['custom']['main'] = queryCustom($conn, $id);
 echo json_encode($final);

My global.js has the following code which makes an AJAX request in jQuery:

$.ajax({
            type: "POST",
            url: "functions.php", // file to reach Ajax call
            dataType: "json", 
            data: { 
                action: 'custom',
                id: id,
                  },
                success:
                function(data) {

                setCustom(data.custom);

I want to know what the data contains in function(data)? The setCustom(data.custom), what does this mean here? Can someone provide me an explanation of this please?

1
  • Please give me an example Commented Mar 13, 2016 at 13:45

2 Answers 2

1

data contains an object literal provided by the server side json_encode(). It's been automatically parsed because the data type is set to json.

example:

PHP:

$final['custom']['main'] = queryCustom($conn, $id);
echo json_encode($final);

Will give the following json string (formatted for better understanding):

{
    "custom": {
        "main":[
            {
                "symbol":"MAP4",
                "official_ID":"112315"
            }
        ]
    }
}

The ajax call above is set to datatype json. jQuery knows if this datatype is set, it will automatically parse the string to an object literal.

Javascript:

$.ajax({
    type: "POST",
    url: "url.php", // file to reach Ajax call
    dataType: "json", 
    success: function(data) {
        var main = data.custom;
        console.log(main); // returns the child object with "main"
    }
});

data contains the initial object, custom is a child object of data and main is a child object of custom.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the informative replay Mr @Tyr. When I echo json_encode($final);, I get the following output: {"custom":{"main":[{"symbol":"MAP4","official_ID":"112315"}]}} Can you edit your answer with regard to my output please ? Then I'll understand at 100%
Awesome understanding ! Thank you loads Mr Tyr.
So what is printed on the console.log is the MAP4 and 112315, right ?
It is. As you can see main is an array (shown by the [ ] braces) which contains an object with MAP4 and 112315. So if you want to have access to the object you have to use main[0].symbol and so on...
0

Success: A function to be called if the request succeeds. The function gets passed three arguments:

  • Data Returned From The Server
  • Status
  • jqXHR (XMLHttpRequest Object)

In your PHP code you've added echo json_encode($final); which will be returned if the request is completed successfully. The response will be passed to data which you can later use in your front-end code.

You can read more about $.ajax() at http://api.jquery.com/jQuery.ajax/

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.